456 questions
129 votes
11 answers
46k views
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 ...
100 votes
5 answers
48k views
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 ...
85 votes
3 answers
38k views
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 ...
73 votes
6 answers
63k views
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 ...
70 votes
6 answers
42k views
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?
69 votes
6 answers
64k views
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 ...
66 votes
6 answers
54k views
Are there any concurrent containers in C++11? [closed]
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-...
47 votes
4 answers
41k views
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 ...
41 votes
8 answers
16k views
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 ...
40 votes
2 answers
51k views
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); }...
32 votes
11 answers
24k views
"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 ...
28 votes
5 answers
21k views
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....
28 votes
6 answers
34k views
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 ...
27 votes
4 answers
15k views
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 ...
21 votes
5 answers
12k views
Is adding tasks to BlockingQueue of ThreadPoolExecutor advisable?
The JavaDoc for ThreadPoolExecutor is unclear on whether it is acceptable to add tasks directly to the BlockingQueue backing the executor. The docs say calling executor.getQueue() is "intended ...