Skip to content
Related Articles

Related Articles

DelayQueue put() method in Java with Examples
  • Last Updated : 23 Dec, 2018

The put(E ele) method of DelayQueue class in Java is used to inserts a given element into the delay queue container. Hetre, E is the type of element maintained by this DelayQueue container.

Syntax:

public void put(E ele)

Parameters: This method accepts only one parameter ele. It is the element which will be inserted into the delay queue.

Return Value: The return type of the method is void and it does not return any value.

Exception:



  • NullPointerException: It is thrown if the given value is null.

Below programs illustrate the put() method of DelayQueue in Java:

Program 1:




// Java program to illustrate the put()
// method in Java
  
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
  
public class GFG {
    public static void main(String args[])
    {
        // Create DelayQueue instance
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        // Initially Delay Queue is empty
        System.out.println("Before calling put() : "
                           + queue.isEmpty());
  
        // Cerate an object of type Delayed
        Delayed ele = new Delayed() {
            public long getDelay(TimeUnit unit)
            {
                return 24; // some value is returned
            }
  
            public int compareTo(Delayed o)
            {
                if (o.getDelay(TimeUnit.DAYS) > 
                              this.getDelay(TimeUnit.DAYS))
                    return 1;
                else if (o.getDelay(TimeUnit.DAYS) ==  
                                this.getDelay(TimeUnit.DAYS))
                    return 0;
                return -1;
            }
        };
  
        // Insert the created object to DelayQueue
        // using the put() method
        queue.put(ele);
  
        // Check if DelayQueue is empty.
        System.out.println("After calling put() : "
                           + queue.isEmpty());
    }
}
Output:
Before calling put() : true
After calling put() : false

Program 2 : Demonstration of NullPointerException.




// Java program to illustrate the Exception thrown
// by put() method of DelayQueue
  
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
  
public class GFG {
    public static void main(String args[])
    {
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        try {
            queue.put(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Output:
java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/DelayQueue.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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :