Member-only story
Java Multithreading, Concurrency, and Parallelism — Part 16
The Conditions and BlockingQueues

In the previous part, we have seen the ReentrantLock
with and without fairness and compared their performances. In this part, we will see a key concept that is used in conjunction with ReentrantLocks
to implement the BlockingQueues
— The Conditions
.
First, we understand what a BlockingQueue
is in the java concurrency framework, look at several implementations, understand what theCondition
object is, and finally implement our own BlockingQueue.
A BlockingQueue
is a type of shared collection that is used to exchange data between two or more threads while causing one or more of the threads to wait/block until the point in time when the data can be exchanged. Typically this kind of blocking happens with bouned buffers. We will use these kinds of collections to implement the Producer-Consumer
patterns. Remember we implemented the bounded buffers with synchronized
, wait
& notify
in Part-10, where one thread produces data, then adds it to a queue, and another thread consumes the data from the queue. A queue provides the means for the producer and the consumer to exchange objects. The java.util.concurrent
package provides several BlockingQueue
implementations that can be classified as two types:
- Bounded Queues
■ ArrayBlockingQueue
■…