Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaBlockingDeque putFirst() method in Java with Examples

BlockingDeque putFirst() method in Java with Examples

The putFirst(E e) method of BlockingDeque 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 BlockingDeque.

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

Note: The putFirst() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.

Below programs illustrate putFirst() method of BlockingDeque:

Program 1:




// Java Program Demonstrate putFirst()
// 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 InterruptedException
    {
  
        // create object of BlockingDeque
        BlockingDeque<Integer> BD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of BlockingDeque
        BD.putFirst(7855642);
        BD.putFirst(35658786);
        BD.putFirst(5278367);
        BD.putFirst(74381793);
  
        // print Deque
        System.out.println("Blocking Deque: " + BD);
    }
}


Output:

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

Program 2:




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


Output:

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

Program 3:




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


Output:

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

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

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

Most Popular

Recent Comments