BlockingDeque addLast() method in Java with Examples
The addLast(E e) method of BlockingDeque inserts the element passed in the parameter to the end of the Deque if there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException.
Syntax:
public void addLast(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
Note: The addLast() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.
Below programs illustrate addLast() method of BlockingDeque:
Program 1:
// Java Program Demonstrate addLast() // 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.addLast( 7855642 ); BD.addLast( 35658786 ); BD.addLast( 5278367 ); BD.addLast( 74381793 ); // before removing print Deque System.out.println( "Blocking Deque: " + BD); } } |
Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Program 2:
// Java Program Demonstrate addLast() // 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.addLast( 7855642 ); BD.addLast( 35658786 ); BD.addLast( 5278367 ); // it is full BD.addLast( 74381793 ); // before removing print Deque System.out.println( "Blocking Deque: " + BD); } } |
Output:
Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335) at GFG.main(GFG.java:25)
Program 3:
// Java Program Demonstrate addLast() // method of BlockingDeque // when nill 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.addLast( 7855642 ); BD.addLast( 35658786 ); BD.addLast( 5278367 ); // NULL BD.addLast( null ); // before removing print queue System.out.println( "Blocking Deque: " + BD); } } |
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 GFG.main(GFG.java:24)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#addLast(E)
Recommended Posts:
- BlockingDeque put() method in Java with Examples
- BlockingDeque contains() method in Java with Examples
- BlockingDeque take() method in Java with Examples
- BlockingDeque add() method in Java with Examples
- BlockingDeque putFirst() method in Java with Examples
- BlockingDeque push() method in Java with examples
- BlockingDeque takeFirst() method in Java with Examples
- BlockingDeque pollLast() method in Java with examples
- BlockingDeque removeLastOccurrence() method in Java with Examples
- BlockingDeque remove() method in Java with Examples
- BlockingDeque takeLast() method in Java with Examples
- BlockingDeque removeFirstOccurrence() method in Java with Examples
- BlockingDeque size() method in Java with Examples
- BlockingDeque offerFirst() method in Java with Examples
- BlockingDeque peek() method in Java with examples
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.