Related Articles

Related Articles

BlockingDeque put() method in Java with Examples
  • Last Updated : 01 Oct, 2019

The put(E e) method of BlockingDeque 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 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 put() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.

Below programs illustrate put() method of BlockingDeque:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate put()
// 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.put(7855642);
        BD.put(35658786);
        BD.put(5278367);
        BD.put(74381793);
  
        // print Deque
        System.out.println("Blocking Deque: " + BD);
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate put()
// 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.put(7855642);
        BD.put(35658786);
        BD.put(5278367);
  
        // throws an exception
        BD.put(null);
  
        // print Dequeue
        System.out.println("Blocking Deque: " + BD);
    }
}

chevron_right


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:24)

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate put()
// 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.put(7855642);
        BD.put(35658786);
        BD.put(5278367);
  
        // throws an exception
        BD.put(4356789);
  
        // print Dequeue
        System.out.println("Blocking Deque: " + BD);
    }
}

chevron_right


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#put(E)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :