Java队列实现,哪一个? [英] Java Queue implementations, which one?

查看:104
本文介绍了Java队列实现,哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Javadoc:


  • ConcurrentLinkedQueue 是许多线程共享对公共集合访问权限的适当选择。此队列不允许空元素。

  • ArrayBlockingQueue 是一个经典的有界缓冲区,其中固定大小的数组保存由生产者插入并由消费者提取的元素。此类支持用于排序等待生产者和消费者线程的可选公平策略。

  • LinkedBlockingQueue 通常具有比基于阵列的队列更高的吞吐量,但在大多数并发应用程序中的可预测性能较差。

  • A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection. This queue does not permit null elements.
  • ArrayBlockingQueue is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. This class supports an optional fairness policy for ordering waiting producer and consumer threads
  • LinkedBlockingQueue typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

我有两个场景,一个需要队列支持许多生产者(线程使用它)与一个消费者,另一个是另一种方式arround。

I have 2 scenarios, one requires the queue to support many producers (threads using it) with one consumer and the other is the other way arround.

我不知道是否应该使用ConcurrentLikedQueue或其他(数组或linkedList实现)。所有这些实现应该是并发的?我的意思是,有人可以解释一下ConcurrentLikedQueue和LinkedBlockingQueue之间的区别是什么?

I am not understanding whether I should use ConcurrentLikedQueue or the other ones (the array or linkedList implementations). Wherent' all this implementations supposed to be concurrent? I mean, can somebody explain me what is the diference between ConcurrentLikedQueue and LinkedBlockingQueue ?

另外,ArrayBlockingQueue中的可选公平策略是什么?

Also, what is the optional fairness policy thing in the ArrayBlockingQueue please ?

推荐答案

基本上它们之间的区别是性能特征和阻塞行为。

Basically the difference between them are performance characteristics and blocking behavior.

首先,ArrayBlockingQueue是固定大小的队列。因此,如果将大小设置为10,并尝试插入第11个元素,则insert语句将阻塞,直到另一个线程移除元素。公平性问题是,如果多个线程尝试同时插入和删除(换句话说,在队列被阻塞期间)。公平算法确保请求的第一个线程是获取的第一个线程。否则,给定的线程可能比其他线程等待更长时间,导致不可预测的行为(有时一个线程只需要几秒钟,因为稍后开始的其他线程首先被处理)。需要权衡的是,它需要管理公平性,减缓吞吐量的开销。

Taking the easiest first, ArrayBlockingQueue is a queue of a fixed size. So if you set the size at 10, and attempt to insert an 11th element, the insert statement will block until another thread removes an element. The fairness issue is what happens if multiple threads try to insert and remove at the same time (in other words during the period when the Queue was blocked). A fairness algorithm ensures that the first thread that asks is the first thread that gets. Otherwise, a given thread may wait longer than other threads, causing unpredictable behavior (sometimes one thread will just take several seconds because other threads that started later got processed first). The trade-off is that it takes overhead to manage the fairness, slowing down the throughput.

LinkedBlockingQueue和ConcurrentLinkedQueue之间最重要的区别是,如果你请求一个元素一个LinkedBlockingQueue和队列是空的,你的线程将等待,直到有东西存在。 ConcurrentLinkedQueue将立即返回一个空队列的行为。

The most important difference between LinkedBlockingQueue and ConcurrentLinkedQueue is that if you request an element from a LinkedBlockingQueue and the queue is empty, your thread will wait until there is something there. A ConcurrentLinkedQueue will return right away with the behavior of an empty queue.

如果你需要阻塞,哪一个依赖。你有许多生产者和一个消费者,它听起来像。另一方面,如果你有很多消费者,只有一个生产者,你可能不需要阻塞行为,并可能很高兴只是让消费者检查队列是否为空,如果是,继续前进。

Which one depends on if you need the blocking. Where you have many producers and one consumer, it sounds like it. On the other hand, where you have many consumers and only one producer, you may not need the blocking behavior, and may be happy to just have the consumers check if the queue is empty and move on if it is.

这篇关于Java队列实现,哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆