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 Delayed.
Returns:
This method does not return anything.
Exception:
Null pointer Exception: if the specified element is null.
Below program to illustrate DelayQueue offer() in Java:
Example:
// Java Program Demonstrate DelayQueue offer() method import java.util.concurrent.*; import java.util.*; // The DelayObject for DelayQueue // It must implement Delayed and // its getDelay() and compareTo() method class DelayObject implements Delayed { private String name; private long time; // Contructor of DelayObject public DelayObject(String name, long delayTime) { this.name = name; this.time = System.currentTimeMillis() + delayTime; } // Implementing getDelay() method of Delayed @Override public long getDelay(TimeUnit unit) { long diff = time - System.currentTimeMillis(); return unit.convert(diff, TimeUnit.MILLISECONDS); } // Implementing compareTo() method of Delayed @Override public int compareTo(Delayed obj) { if (this.time < ((DelayObject)obj).time) { return -1; } if (this.time > ((DelayObject)obj).time) { return 1; } return 0; } // Implementing toString() method of Delayed @Override public String toString() { return "\n{" + " " + name + ", time=" + time + "}"; } } // Driver Class public class GFG { public static void main(String[] args) throws InterruptedException { // create object of DelayQueue // using DelayQueue() constructor BlockingQueue<DelayObject> DQ = new DelayQueue<DelayObject>(); // Add numbers to end of DelayQueue // using add() method DQ.add(new DelayObject("A", 1)); DQ.add(new DelayObject("B", 2)); // Print delayqueue System.out.println("Original DelayQueue: " + DQ + "\n"); // Now insert elements using offer method DQ.offer(new DelayObject("C", 10)); DQ.offer(new DelayObject("D", 11)); DQ.offer(new DelayObject("E", 15)); DQ.offer(new DelayObject("F", 17)); // print queue System.out.println("After insertion DelayQueue: " + DQ); } } |
Original DelayQueue: [
{ A, time=1545817395066},
{ B, time=1545817395067}]
After insertion DelayQueue: [
{ A, time=1545817395066},
{ B, time=1545817395067},
{ C, time=1545817395076},
{ D, time=1545817395077},
{ E, time=1545817395081},
{ F, time=1545817395083}]
Recommended Posts:
- DelayQueue put() method in Java with Examples
- DelayQueue add() method in Java with Examples
- DelayQueue take() method in Java with Examples
- DelayQueue remainingCapacity() method in Java with Examples
- DelayQueue drainTo() method in Java with Examples
- DelayQueue peak() method in Java with Examples
- DelayQueue toArray() method in Java with Examples
- DelayQueue size() method in Java with Examples
- DelayQueue iterator() method in Java with Examples
- DelayQueue poll() method in Java with Examples
- ConcurrentLinkedDeque offer() method in Java with Examples
- BlockingQueue offer() method in Java with examples
- DelayQueue clear() method in Java
- DelayQueue remove() method in Java
- BlockingDeque offer() function in Java with Examples
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.



