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 info G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions Java-BlockingQueue Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like