BlockingQueue add() in Java with examples Last Updated : 19 Feb, 2021 Comments Improve Suggest changes Like Article Like Report The add(E e) method of BlockingQueue interface inserts the element passed in the parameter to the end of the Queue is there is space. If the BlockingQueue os capacity restricted and no space is left for insertion, it returns an IllegalStateException. Syntax: public void add(E e)Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the BlockingQueue. Returns: This method returns true on successful insertion. Exception: IllegalStateException: if the element cannot be added at this time due to capacity restrictionsNullPointerException: if the specified element is nullNote: The add() method of BlockingQueue has been inherited from the Queue class in Java.Below programs illustrate add() method of BlockingQueue: Program 1: Java // Java Program Demonstrate add() // method of BlockingQueue import java.util.*; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of BlockingQueue BlockingQueue<Integer> BQ = new LinkedBlockingDeque<Integer>(); // Add numbers to the BlockingQueue BQ.add(7855642); BQ.add(35658786); BQ.add(5278367); BQ.add(74381793); // before removing print BlockingQueue System.out.println("Blocking Queue: " + BQ); } } OutputBlocking Queue: [7855642, 35658786, 5278367, 74381793] Program 2: Java // Java Program Demonstrate add() // method of LinkedBlockingDeque // when null is inserted import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> BQ = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque BQ.add(7855642); BQ.add(35658786); BQ.add(5278367); // NULL BQ.add(null); // before removing print Deque System.out.println("Linked Blocking Deque: " + BQ); } } Output: Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335) at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633) at GFG.main(GFG.java:25)Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#add(E) Comment More infoAdvertise with us Next Article BlockingQueue add() in Java with examples gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions Java-BlockingQueue Practice Tags : JavaJava-Collections Similar Reads BlockingDeque add() method in Java with Examples The add(E e) method of BlockingDeque inserts the element passed in the parameter to the end of the Deque is there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. It works exactly in the same way as addLast() method does. 2 min read BlockingDeque addLast() method in Java with Examples The addLast(E e) method of BlockingDeque inserts the element passed in the parameter to the end of the Deque if there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. Syntax: public void addLast(E e) Parameters: This metho 2 min read BlockingDeque addFirst() method in Java with Examples The addFirst(E e) method of BlockingDeque inserts the element passed in the parameter to the front of the Deque if there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. Syntax: public void addFirst(E e) Parameters: This m 2 min read BlockingQueue put() method in Java with examples The put(E e) method of BlockingQueue interface inserts element passed as parameter to method at the tail of this BlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to BlockingQueue. 3 min read AbstractQueue in Java with Examples The AbstractQueue class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It provides skeletal implementations of some Queue operations. The implementations in this class are appropriate when the base implementation does not 11 min read BlockingQueue offer() method in Java with examples There are two types of offer() method for BlockingQueue interface:Note: The offer() method of BlockingQueue has been inherited from the Queue class in Java. offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of BlockingQueue inserts the element passed as param 6 min read DelayQueue add() method in Java with Examples The add(E ele) method of DelayQueue class in Java is used to insert the given element into the delay queue and returns true if the element has been successfully inserted. Here, E refers to the type of elements maintained by this DelayQueue collection.Syntax: public boolean add(E ele) Parameters: Thi 2 min read BlockingDeque put() method in Java with Examples The put(E e) method of BlockingDeque inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque). If the Deque is capacity restricted, then it will wait for the space to become available. Syntax: public void put(E e) Parameters: This method acce 2 min read BlockingDeque push() method in Java with examples The push(E e) method of BlockingDeque pushes an element onto the stack represented by this deque. It inserts the element passed in the parameter to the front of the Deque if there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateExce 2 min read BlockingDeque element() method in java with examples The element() method of BlockingDeque returns the element at the front the container. It does not delete the element in the container. This method returns the head of the queue represented by this deque. Syntax: public void element() Parameters: This method does not accept any parameter. Returns: Th 2 min read Like