The put(E e) method of LinkedBlockingDeque inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque). If the Deque is capacity restricted, then it will wait for the space to become available.
Syntax:
public void put(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted at the end 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 put() method of LinkedBlockingDeque:
Program 1:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.put( 7855642 );
LBD.put( 35658786 );
LBD.put( 5278367 );
LBD.put( null );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Program 2:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>( 3 );
LBD.put( 7855642 );
LBD.put( 35658786 );
LBD.put( 5278367 );
LBD.put( 4356789 );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.putLast(LinkedBlockingDeque.java:390)
at java.util.concurrent.LinkedBlockingDeque.put(LinkedBlockingDeque.java:649)
at GFG.main(GFG.java:22)
Program 3:
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#put-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 :
17 Sep, 2018
Like Article
Save Article