LinkedBlockingDeque add() method in Java
The add(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the end of the Deque is there is space. If the LinkedBlockingDeque 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.
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 LinkedBlockingDeque.
Returns: This method does not returns anything.
Exception:
- IllegalStateException: if the element cannot be added at this time due to capacity restrictions
- NullPointerException: if the specified element is null
Below programs illustrate add() method of LinkedBlockingDeque:
Program 1:
// Java Program Demonstrate add() // method of LinkedBlockingDeque 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> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add(7855642); LBD.add(35658786); LBD.add(5278367); LBD.add(74381793); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } } |
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Program 2:
// Java Program Demonstrate add() // method of LinkedBlockingDeque // when it is Full import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque // size of list LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(3); // Add numbers to end of LinkedBlockingDeque LBD.add(7855642); LBD.add(35658786); LBD.add(5278367); // it is full LBD.add(74381793); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } } |
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:
// 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> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add(7855642); LBD.add(35658786); LBD.add(5278367); // NULL LBD.add(null); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } } |
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334)
at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
at GFG.main(GFG.java:23)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#add(E)
Recommended Posts:
- LinkedBlockingDeque contains() method in Java
- LinkedBlockingDeque take() method in Java
- LinkedBlockingDeque put() method in Java
- LinkedBlockingDeque pop() method in Java
- LinkedBlockingDeque removeLast() method in Java
- LinkedBlockingDeque push() method in Java
- LinkedBlockingDeque drainTo() method in Java with Example
- LinkedBlockingDeque getLast() method in Java
- LinkedBlockingDeque peek() method in Java
- LinkedBlockingDeque offerLast() method in Java
- LinkedBlockingDeque offerFirst() method in Java
- LinkedBlockingDeque offer() method in Java
- LinkedBlockingDeque toArray() method in Java with Example
- LinkedBlockingDeque takeFirst() method in Java
- LinkedBlockingDeque remove() method in Java
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.



