BlockingDeque add() method in Java with Examples
Last Updated :
01 Oct, 2019
Improve
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.
Syntax:
Java
Java
Output:
Java
Output:
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 BlockingDeque. 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
// Java Program Demonstrate add()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.add(7855642);
BD.add(35658786);
BD.add(5278367);
BD.add(74381793);
// before removing print queue
System.out.println("Blocking Deque: " + BD);
}
}
26
1
// Java Program Demonstrate add()
2
// method of BlockingDeque
3
4
import java.util.concurrent.LinkedBlockingDeque;
5
import java.util.concurrent.BlockingDeque;
6
import java.util.*;
7
8
public class GFG {
9
public static void main(String[] args)
10
throws IllegalStateException
11
{
12
13
// create object of BlockingDeque
14
BlockingDeque<Integer> BD
15
= new LinkedBlockingDeque<Integer>();
16
17
// Add numbers to end of BlockingDeque
18
BD.add(7855642);
19
BD.add(35658786);
20
BD.add(5278367);
21
BD.add(74381793);
22
23
// before removing print queue
24
System.out.println("Blocking Deque: " + BD);
25
}
26
}
Output:
Program 2:
Blocking Deque: [7855642, 35658786, 5278367, 74381793]
// Java Program Demonstrate add()
// method of BlockingDeque
// when it is Full
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
// size of list
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>(3);
// Add numbers to end of BlockingDeque
BD.add(7855642);
BD.add(35658786);
BD.add(5278367);
// it is full
BD.add(74381793);
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
}
}
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:24)Program 3:
// Java Program Demonstrate add()
// method of BlockingDeque
// when null is inserted
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.add(7855642);
BD.add(35658786);
BD.add(5278367);
// NULL
BD.add(null);
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
}
}
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/BlockingDeque.html#add(E)