DelayQueue put() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

Before calling put() : true
After calling put() : false

Program 2 : Demonstration of NullPointerException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

java.lang.NullPointerException

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.