LinkedBlockingDeque putFirst() method in Java

The putFirst(E e) method of LinkedBlockingDeque inserts the specified element at the front of the queue represented by this deque. If the Deque is capacity restricted, then it will wait for the space to become available.

Syntax:

public void putFirst(E e)

Parameters: This method accepts a mandatory parameter e which is the element to be inserted at the front of the LinkedBlockingDeque.

Returns: This method does not return anything.

Exceptions: The program throws two exceptions as shown below:



  • NullPointerException – if the specified element is null
  • InterruptedException – if interrupted while waiting

Below programs illustrate putFirst() method of LinkedBlockingDeque:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate putFirst()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.putFirst(7855642);
        LBD.putFirst(35658786);
        LBD.putFirst(5278367);
        LBD.putFirst(74381793);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}

chevron_right


Output:

Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate putFirst()
// method of LinkedBlockingDeque
// throwing NullPointerException
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.putFirst(7855642);
        LBD.putFirst(35658786);
        LBD.putFirst(5278367);
  
        // throws an exception
        LBD.putFirst(null);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}

chevron_right


Output:

Exception in thread "main" java.lang.NullPointerException
    at java.util.concurrent.LinkedBlockingDeque.putFirst(LinkedBlockingDeque.java:373)
    at GFG.main(GFG.java:23)

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate putFirst()
// method of LinkedBlockingDeque
// when capacity exceeded
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>(3);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.putFirst(7855642);
        LBD.putFirst(35658786);
        LBD.putFirst(5278367);
  
        // throws an exception
        LBD.putFirst(4356789);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}

chevron_right


Output:

Runtime Errors:
Max real time limit exceeded due to either by
heavy load on server or by using sleep function

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#putFirst-E-

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.