The addFirst(E e) method of BlockingDeque inserts the element passed in the parameter to the front 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 addFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the front 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 addFirst() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.
Below programs illustrate addFirst() method of BlockingQueue:
Program 1:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
BD.addFirst( 7855642 );
BD.addFirst( 35658786 );
BD.addFirst( 5278367 );
BD.addFirst( 74381793 );
System.out.println( "Blocking Deque: " + BD);
}
}
|
Output:
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Program 2:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>( 3 );
BD.addFirst( 7855642 );
BD.addFirst( 35658786 );
BD.addFirst( 5278367 );
BD.addFirst( 74381793 );
System.out.println( "Blocking Deque: " + BD);
}
}
|
Output:
Exception in thread "main" java.lang.IllegalStateException: Deque full
at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326)
at GFG.main(GFG.java:24)
Program 3:
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
BD.addFirst( 7855642 );
BD.addFirst( 35658786 );
BD.addFirst( 5278367 );
BD.addFirst( null );
System.out.println( "Blocking Deque: " + BD);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.offerFirst(LinkedBlockingDeque.java:342)
at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:325)
at GFG.main(GFG.java:24)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#addFirst(E)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Oct, 2019
Like Article
Save Article