In this article, you will learn-
Java BlockingQueue
In this tutorial, we will find out about the Java BlockingQueue interface and its methods.
The BlockingQueue interface of the Java Collections system broadens the Queue interface. It allows any activity to stand by until it very well may be effectively performed.
For instance, if we need to erase a component from an unfilled line, at that point the obstructing line permits the erase activity to stand by until the line contains a few components to be erased.
Classes that Implement BlockingQueue
Since BlockingQueue is an interface, we cannot provide the direct implementation of it.
In order to use the functionality of the BlockingQueue, we need to use classes that implement it.
How to use blocking queues?
We must import the java.util.concurrent.BlockingQueue package in order to use BlockingQueue.
// Array implementation of BlockingQueue
BlockingQueue<String> animal1 = new ArraryBlockingQueue<>();
// LinkedList implementation of BlockingQueue
BlockingQueue<String> animal2 = new LinkedBlockingQueue<>();
Here, we have created objects animal1 and animal2 of classes ArrayBlockingQueue and LinkedBlockingQueue, respectively. These objects can use the functionalities of the BlockingQueue interface.
Methods of BlockingQueue
Based on whether a queue is full or empty, methods of a blocking queue can be divided into 3 categories:
Methods that throw an exception
- add() – Inserts an element to the blocking queue at the end of the queue. Throws an exception if the queue is full.
- element() – Returns the head of the blocking queue. Throws an exception if the queue is empty.
- remove() – Removes an element from the blocking queue. Throws an exception if the queue is empty.
Methods that return some value
- offer() – Inserts the specified element to the blocking queue at the end of the queue. Returns false if the queue is full.
- peek() – Returns the head of the blocking queue. Returns null if the queue is empty.
- poll() – Removes an element from the blocking queue. Returns null if the queue is empty.
More on offer() and poll()
The offer() and poll() method can be used with timeouts. That is, we can pass time units as a parameter. For example,
offer(value, 100, milliseconds)
Here,
- value is the element to be inserted into the queue
- And we have set a timeout of 100 milliseconds
This implies the offer() strategy will attempt to insert a component to the hindering queue for 100 milliseconds. In the event that the component can’t be embedded in 100 milliseconds, the strategy returns false.
Note: Instead of milliseconds, we can also use these time units: days, hours, minutes, seconds, microseconds and nanoseconds in offer() and poll() methods.
Methods that blocks the operation
The BlockingQueue also provides methods to block the operations and wait if the queue is full or empty.
- put() – Inserts an element to the blocking queue. If the queue is full, it will wait until the queue has space to insert an element.
- take() – Removes and returns an element from the blocking queue. If the queue is empty, it will wait until the queue has elements to be deleted.
Assume, we need to insert components into a queue. If the queue is full, at that point the put() strategy will stand by until the line has space to embed components.
Additionally, if we need to erase components from a queue. if the queue is unfilled, at that point the take() strategy will stand by until the line contains components to be erased.
Implementation of BlockingQueue in ArrayBlockingQueue
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
// Create a blocking queue using the ArrayBlockingQueue
BlockingQueue<Integer> numbers = new ArrayBlockingQueue<>(5);
try {
// Insert element to blocking queue
numbers.put(2);
numbers.put(1);
numbers.put(3);
System.out.println("BLockingQueue: " + numbers);
// Remove Elements from blocking queue
int removedNumber = numbers.take();
System.out.println("Removed Number: " + removedNumber);
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
BlockingQueue: [2, 1, 3]
Removed Element: 2
Why BlockingQueue?
In Java, BlockingQueue is considered as the thread-safe assortment. It is on the grounds that it very well may be useful in multi-threading activities.
Assume one thread is inserting components to the queue and another string is eliminating components from the queue.
Presently, if the main thread runs more slow, at that point the hindering line can make the subsequent string stand by until the principal string finishes its activity.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.