BlockingQueue add() in Java with examples

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 succesfull insertion.

Exception:

  • 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:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate add()
// method of BlockingQueue
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.*;
  
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);
    }
}

chevron_right


Output:

Blocking Queue: [7855642, 35658786, 5278367, 74381793]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

 

chevron_right


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:23)

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate add()
// method of LinkedBlockingDeque
// when null is inserted
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
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);
    }
}

chevron_right


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)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.