DelayQueue add() method in Java with Examples

The add(E ele) method of DelayQueue class in Java is used to insert the given element into the delay queue and returns true if the element has been successfully inserted. Here, E refers to the type of elements maintained by this DelayQueue collection.

Syntax:

public boolean add(E ele)

Parameters: This method takes only one parameter ele. It refers to the element which will be inserted into the delay queue.

Return Value: It returns a boolean value which is true if the element has been added successfully otherwise it returns flase.

Exception:

  • NullPointerException: This method throws a NullPointerException if a NULL is tried to inserted in this DelayQueue.

Below programs illustrate the add() method of DelayQueue class:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the add()
// 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 a DelayQueue instance
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        // Create an instance of Delayed
        Delayed obj = 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;
            }
        };
  
        // Use the add() method to add obj to
        // the empty DelayQueue instance
        queue.add(obj);
  
        System.out.println("Size of the queue : " + queue.size());
    }
}

chevron_right


Output:

Size of the queue : 1

Program 2: Program to demonstrate the NullPointerException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the Exception
// thrown by add() method of
// DelayQueue classs
  
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 an instance of DelayQueue
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        // Try to add NULL to the queue
        try {
            queue.add(null);
        
  
        // Catch Exception
        catch (Exception e) {
  
            // Print Exception raised
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.NullPointerException


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.