Monday, June 15, 2026
HomeLanguagesJavaLinkedBlockingDeque push() method in Java

LinkedBlockingDeque push() method in Java

The push(E e) method of LinkedBlockingDeque pushes an element onto the stack represented by this deque. It inserts the element passed in the parameter to the front of the Deque if there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. This function is similar to that of addFirst().

Syntax:

public void push(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 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 push() method of LinkedBlockingDeque:

Program 1:




// Java Program Demonstrate push()
// 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.push(7855642);
        LBD.push(35658786);
        LBD.push(5278367);
        LBD.push(74381793);
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}


Output:

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

Program 2:




// Java Program Demonstrate push()
// 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.push(7855642);
        LBD.push(35658786);
        LBD.push(5278367);
  
        // it is full
        LBD.push(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.addFirst(LinkedBlockingDeque.java:326)
    at java.util.concurrent.LinkedBlockingDeque.push(LinkedBlockingDeque.java:770)
    at GFG.main(GFG.java:23)

Program 3:




// Java Program Demonstrate push()
// 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.push(7855642);
        LBD.push(35658786);
        LBD.push(5278367);
  
        // NULL
        LBD.push(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.offerFirst(LinkedBlockingDeque.java:342)
    at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:325)
    at java.util.concurrent.LinkedBlockingDeque.push(LinkedBlockingDeque.java:770)
    at GFG.main(GFG.java:23)

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#push(E)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS