Questions tagged [blockingqueue]

A queue data structure that that waits (suspends the current thread) for the queue to become non-empty when retrieving an element and for space to become available in the queue when storing an element.

A queue data structure that waits (suspends the current thread) for the queue to become non-empty when retrieving an element, or for space to become available in the queue when storing an element.

References

458 questions
118
votes
10 answers

How to get the ThreadPoolExecutor to increase threads to max before queueing?

I've been frustrated for some time with the default behavior of ThreadPoolExecutor which backs the ExecutorService thread-pools that so many of us use. To quote from the Javadocs: If there are more than corePoolSize but less than maximumPoolSize…
Gray
  • 115,027
  • 24
  • 293
  • 354
97
votes
5 answers

How to interrupt a BlockingQueue which is blocking on take()?

I have a class that takes objects from a BlockingQueue and processes them by calling take() in a continuous loop. At some point I know that no more objects will be added to the queue. How do I interrupt the take() method so that it stops…
MCS
  • 22,113
  • 20
  • 62
  • 76
79
votes
2 answers

When to prefer LinkedBlockingQueue over ArrayBlockingQueue?

When to prefer LinkedBlockingQueue over ArrayBlockingQueue? Which data structure to use among LinkedBlockingQueue and ArrayBlockingQueue when: You want an efficient read and write should have lesser memory footprints Although there is a similar…
Vaibhav Gupta
  • 1,035
  • 1
  • 8
  • 16
72
votes
6 answers

Blocking queue and multi-threaded consumer, how to know when to stop

I have a single thread producer which creates some task objects which are then added into an ArrayBlockingQueue (which is of fixed size). I also start a multi-threaded consumer. This is build as a fixed thread pool…
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
64
votes
6 answers

When should I use SynchronousQueue over LinkedBlockingQueue

new SynchronousQueue() new LinkedBlockingQueue(1) What is the difference? When I should use SynchronousQueue against LinkedBlockingQueue with capacity 1?
Anton
  • 5,831
  • 3
  • 35
  • 45
64
votes
6 answers

Are there any concurrent containers in C++11?

In particular, I am looking for a blocking queue. Is there such a thing in C++11? If not, what are my other options? I really don't want to go down to the thread level myself anymore. Way too error-prone.
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
63
votes
6 answers

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

I am working on a multithreaded project in which I need to spawn multiple threads to measure the end to end performance of my client code, as I'm doing Load and Performance testing. So I created the below code which is using ExecutorService. Below…
user1813228
46
votes
4 answers

What is the Difference between ArrayBlockingQueue and LinkedBlockingQueue

What scenarios is it better to use an ArrayBlockingQueue and when is it better to use a LinkedBlockingQueue? If LinkedBlockingQueue default capacity is equal to MAX Integer, is it really helpful to use it as BlockingQueue with default capacity?
Java_Jack
  • 577
  • 1
  • 5
  • 8
39
votes
2 answers

Java BlockingQueue take() vs poll()

When consuming values from a Queue in an infinite loop -- what would be more efficient: Blocking on the Queue until a value is available via take() while (value = queue.take()) { doSomething(value); } Sleeping for n milliseconds and checking if…
isapir
  • 21,295
  • 13
  • 115
  • 116
39
votes
8 answers

java BlockingQueue does not have a blocking peek?

I have a blocking queue of objects. I want to write a thread that blocks till there is a object on the queue. Similar to the functionality provided by BlockingQueue.take(). However, since I do not know if I will be able to process the object…
rouble
  • 16,364
  • 16
  • 107
  • 102
32
votes
10 answers

"Closing" a blocking queue

I’m using java.util.concurrent.BlockingQueue in a very simple producer-consumer scenario. E.g. this pseudo code depicts the consumer part: class QueueConsumer implements Runnable { @Override public void run() { while(true) …
Lachezar Balev
  • 11,498
  • 9
  • 49
  • 72
27
votes
5 answers

ScheduledExecutorService with variable delay

Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them. public void scheduleTask(int delay, TimeUnit timeUnit) { scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay,…
parkr
  • 3,188
  • 5
  • 35
  • 39
27
votes
6 answers

How to block until a BlockingQueue is empty?

I'm looking for a way to block until a BlockingQueue is empty. I know that, in a multithreaded environment, as long as there are producers putting items into the BlockingQueue, there can be situations in which the queue becomes empty and a few…
gd1
  • 11,300
  • 7
  • 49
  • 88
23
votes
4 answers

Equivalent of Go channel in Java

I have a requirement where I need to read from a set of Blocking queues. The blocking queues are created by the Library I am using. My code has to read from the queues. I don't want to create a reader thread for each of these blocking queues. Rather…
Nipun Talukdar
  • 4,975
  • 6
  • 30
  • 42
21
votes
5 answers

Java BlockingQueue with batching?

I am interested in a data structure identical to the Java BlockingQueue, with the exception that it must be able to batch objects in the queue. In other words, I would like the producer to be able to put objects into the queue, but have the consumer…
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
1
2 3
30 31