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.
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.
- IllegalStateException: if the element cannot be added at this time due to capacity restrictions
- NullPointerException: if the specified element is null
Note: The add() method of BlockingQueue has been inherited from the Queue class in Java.
Below programs illustrate add() method of BlockingQueue:
Blocking Queue: [7855642, 35658786, 5278367, 74381793]
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)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.