Curriculum
In Java, a BlockingQueue is a type of queue that blocks when you try to retrieve an element from it and the queue is empty, or when you try to add an element to it and the queue is full. The idea behind a BlockingQueue is to provide a way for one thread to wait until another thread has completed a specific task or produced a specific result.
The BlockingQueue interface is part of the java.util.concurrent package, which provides a set of high-level concurrency utilities that help you write concurrent and multithreaded programs in Java. The BlockingQueue interface extends the Queue interface and defines several methods for adding, removing, and examining elements in the queue. The most important methods of the BlockingQueue interface are:
put(E e): Inserts the specified element into the queue, waiting if necessary for space to become available.take(): Retrieves and removes the head of the queue, waiting if necessary until an element becomes available.The BlockingQueue interface has several implementations in Java, including ArrayBlockingQueue, LinkedBlockingQueue, and PriorityBlockingQueue. Let’s take a look at ArrayBlockingQueue as an example:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueExample {
public static void main(String[] args) {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(5);
new Thread(() -> {
try {
queue.put("A");
queue.put("B");
queue.put("C");
queue.put("D");
queue.put("E");
System.out.println("Added A, B, C, D, E to queue");
queue.put("F"); // This will block until space becomes available
System.out.println("Added F to queue");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
Thread.sleep(5000); // Wait for 5 seconds
String element = queue.take();
System.out.println("Removed " + element + " from queue");
element = queue.take();
System.out.println("Removed " + element + " from queue");
element = queue.take();
System.out.println("Removed " + element + " from queue");
element = queue.take();
System.out.println("Removed " + element + " from queue");
element = queue.take();
System.out.println("Removed " + element + " from queue");
element = queue.take(); // This will block until an element becomes available
System.out.println("Removed " + element + " from queue");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
In this example, we create a BlockingQueue of strings with a capacity of 5 elements. We then create two threads: one that adds elements to the queue and one that removes elements from the queue. The first thread adds 5 elements to the queue and then tries to add a sixth element, which will block until the second thread removes an element from the queue. The second thread waits for 5 seconds and then removes 5 elements from the queue one by one, blocking if necessary until an element becomes available.
The output of this program will be something like:
Added A, B, C, D, E to queue Removed A from queue Removed B from queue Removed C from queue Removed D from queue Removed E from queue Removed F from queue