A queue is a data structure that follows the first-in, first-out (FIFO) principle. A blocking queue is a queue that blocks when an operation is attempted on a full queue or an empty queue. The operations on a queue and a blocking queue are similar and are applied accordingly.
This article will demonstrate the basic operation of blocking queues in Java.
Basic Operations in Blocking Queue in Java
To perform larger operations on a blocking queue, it is necessary to understand the basic operations of a blocking queue. The basic operations include:
- Adding elements: using the add() and put() method.
- Deleting elements: using the take(), remove(), and poll() methods.
- Accessing elements: using contains() and elements() methods.
- Retrieving elements: using the next() method.
Operation 1: Addition of Elements in BlockingQueue
The add() method of a blocking queue adds an element to the end of the queue; if the queue is full, the add() method will block until there is space available in the queue.
//Import the Java package for blocking the queue
import java.util.concurrent.*;
class blocking{
public static void main(String[] args)
throws IllegalStateException{
//Create a String declared in the linked Blocking queue
BlockingQueue<String> str
= new LinkedBlockingQueue<String>();
//Enter Strings
str.add("Hello");
str.add("to");
str.add("Programming");
// Print queue that has added the elements
System.out.println("The elements in the BlockingQueue are : \n" + str);
}
}
In the code above we have simply implemented the add() method using the LinkedBlockingQueue in Java.
Output

In the above output, the output has been printed using the add() method of BlockingQueue in Java.
Operation 2: Deleting Elements in BlockingQueue
To remove the elements in the Blocking queue the remove() method is used. The string or integer can be easily removed by declaring the items to be removed. Along with the remove() method the two methods are listed below:
poll() Method: The poll() method gets and removes the first element from the queue.
take() Method: The take() method removes the initial element from the queue.
//Import the Java package for blockingqueue
import java.util.concurrent.*;
class blocking{
public static void main(String[] args) {
//Create a String declared in the linked Blocking queue
BlockingQueue<Integer> addint
= new LinkedBlockingQueue<Integer>();
//Enter Strings
addint.add(89);
addint.add(99);
addint.add(150);
addint.add(100);
// Print queue of elements before removing elements
System.out.println("The elements in the BlockingQueue before are : \n" + addint);
addint.remove(99);
addint.remove(100);
//Print queue after deleting
System.out.println("The elements in the BlockingQueue after removing are : \n" + addint);
}
}
In the above code, two integer values have been deleted through the implementation of the remove() method of Java.
Output

Operation 3: Accessing Elements in BlockingQueue
This method is responsible for accessing a certain element from the Queue. Using three different ways the elements from the queue can be accessed through contain (), element(), and peek().
import java.util.concurrent.* ;
//Declare Class to access the present elements
class Accesingelements {
public static void main( String[ ] args )
{
//Declare the Strings and add using the add() method.
BlockingQueue<String> newstr = new LinkedBlockingQueue<String>( );
newstr.add("This");
newstr.add("Is");
newstr.add("programming");
newstr.add("!");
//Print the elements present in the blocking queue
System.out.println("The elements in the Blocking queue are:"+newstr);
//Check if the entered element is present in the Blocking Queue.
if (newstr.contains("programming"))
System.out.println("The string Programming is present in the queue.");
else
System.out.println("The entered string is not in the queue");
}
}
In the above code, the “.conatins()” method helps in accessing the value and informing whether the element is present in the queue or not.
Output

Operation 4: Iterating Elements in BlockingQueue
To iterate through the queue the iterator has been used and the next() method helps to iterate over the elements in the queue.
import java.util.Iterator;
import java.util.concurrent.*;
class IteratingThroughElements {
public static void main(String[] args) {
//Add the values
BlockingQueue<Integer> addvalue = new LinkedBlockingQueue<Integer>();
addvalue.add(32);
addvalue.add(45);
addvalue.add(56);
addvalue.add(99);
//Use the iterator to iterate over the elements
Iterator<Integer>addvalueiter = addvalue.iterator();
//Print the elements that are present in the Queue.
System.out.println("The elements are:");
for(int j = 0; j<addvalue.size(); j++)
{
System.out.print(addvalueiter.next()+ " ");
}
}
}
In the code above, the values have been added using the add() method, and the iterator runs/iterates over the queue. Using the next() method, the queue elements are returned.
Output

In the above output, the elements in the array have been iterated and returned.
Conclusion
To understand the mechanism of blocking queues, there is a need to understand the basic operation of the BlockingQueue. The basic operations are adding, deleting, accessing, and retrieving the elements in the queue. In this article, we have demonstrated in detail the implementation of basic operations on Blockingqueue in Java that will help to perform the larger operations.