我应该在Java中使用哪个并发队列实现? [英] Which concurrent Queue implementation should I use in Java?

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

问题描述

来自JavaDocs:

From the JavaDocs:


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

  • 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.

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

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 around.

我不明白使用哪种实现方式。有人可以解释一下这些差异是什么吗?

I do not understand which implementation to use. Can somebody explain what the differences are?

此外, ArrayBlockingQueue 中的可选公平政策是什么?

Also, what is the 'optional fairness policy' in the ArrayBlockingQueue?

推荐答案

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

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天全站免登陆