BlockingDeque takeFirst() method in Java with Examples Last Updated : 14 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The takeFirst() method of BlockingDeque returns and removes the first element of the Deque container from it, waiting if necessary until an element becomes available.. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E takeFirst() Returns: This method returns the first element of the Deque container, waiting if necessary until an element becomes available. Exception: The function throws an InterruptedException if it is interrupted while waiting. Note: The takeFirst() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java. Below programs illustrate takeFirst() method of LinkedBlockingDeque: Program 1: Java // Java Program to demonstrate takeFirst() // method of BlockingDeque import java.util.concurrent.BlockingDeque; 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.add(7855642); BD.add(35658786); BD.add(5278367); BD.add(74381793); // print Deque System.out.println("Blocking Deque: " + BD); // removes the front element and prints it System.out.println("Head of Blocking Deque: " + BD.takeFirst()); // prints the Deque System.out.println("Blocking Deque: " + BD); } } Output: Blocking Deque: [7855642, 35658786, 5278367, 74381793] Head of Linked Deque: 7855642 Blocking Deque: [35658786, 5278367, 74381793] Program 2: To demonstrate InterruptedException Java // Java Program to demonstrate takeFirst() // 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.add(7855642); BD.add(35658786); BD.add(5278367); BD.add(74381793); // print Dequeue System.out.println("Blocking Deque: " + BD); BD.clear(); // throws error as the list is empty and it // is interrupted while waiting System.out.println("Head of Blocking Deque: " + BD.takeFirst()); } } 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/7/docs/api/java/util/concurrent/BlockingDeque.html#takeFirst() Comment More infoAdvertise with us Next Article BlockingDeque takeFirst() method in Java with Examples gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-BlockingDeque Practice Tags : Java Similar Reads 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 paramet 2 min read BlockingDeque take() method in Java with Examples The take() method of BlockingDeque returns and removes the head of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E take() Returns: This method returns the head of the Deque container. Exception: The function throws a Interru 2 min read BlockingDeque takeLast() method in Java with Examples The takeLast() method of BlockingDeque returns and removes the tail of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax:  public E takeLast() Returns: This method returns the tail(last element) of the Deque container. Exception: The 2 min read BlockingDeque offerFirst() method in Java with Examples The offerFirst(E e) method of BlockingDeque inserts the element passed in the parameter at the front of the Deque container. If the container's capacity has exceeded, then it does not returns an exception as in case of add() and addFirst() function. Syntax: public boolean offerFirst(E e) Parameters: 2 min read BlockingDeque pollFirst() method in Java with examples The pollFirst() method of BlockingDeque returns the front element in the Deque container, and deletes it. It returns null if the container is empty. Syntax: public E pollFirst() Parameters: This method does not accept any parameters. Returns: This method returns front element in the Deque container 2 min read Like