DelayQueue put() method in Java with Examples Last Updated : 11 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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()); // Create 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 // 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) Comment More infoAdvertise with us Next Article DelayQueue poll() method in Java with Examples P psil123 Follow Improve Article Tags : Java Java - util package java-DelayQueue Practice Tags : Java Similar Reads DelayQueue take() method in Java with Examples The take() method of DelayQueue is used to retrieve head of the DelayQueue and also removes it. Thus, the size of the DelayQueue is reduced. This function waits if an element with expired delay is available on this queue. Syntax: public E take () Parameters: This method accepts no parameters.Return 2 min read DelayQueue poll() method in Java with Examples The poll() method of DelayQueue is used to retrieve head of the DelayQueue. This method also removes the head from the DelayQueue. Thus when poll method is called, DelayQueue size is reduced by one.Syntax: public E poll () Parameters: This method does not accept any parameter.Returns: This method re 3 min read DelayQueue peek() method in Java with Examples The peek() method of DelayQueue is used to retrieve head of the DelayQueue, but does not remove it, as in case of poll() method where head is removed from the DelayQueue. Syntax: public E peek () Parameters: This method does not accept any parameter. Returns: This method returns the head of the Dela 3 min read DelayQueue size() method in Java with Examples The size() method of DelayQueue class in Java is used to return the number of elements present in the given queue. If the number of elements is greater than Integer.MAX_VALUE then Integer.MAX_VALUE is returned as the size of this DelayQueue.Syntax: public int size() Parameters: It does not takes in 1 min read DelayQueue offer() method in Java with Examples The offer() method of DelayQueue is used to insert specified element in the delay queue. It acts similar to add() method of DelayQueue.Syntax: public boolean offer (E e) Parameters: DelayQueue accepts only those elements that belong to a class of type Delayed. So, this element E should be of type De 2 min read 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: Thi 2 min read Like