The DelayQueue class belongs to java.util.concurrent package. DelayQueue implements the BlockingQueue interface. DelayQueue is a specialized Priority Queue that orders elements supported their delay time. It means that only those elements can be taken from the queue whose time has expired. DelayQueue head contains the element that has expired within the least time.
An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. The head of the queue is that Delayed element whose delay expired furthest in the past. If no delay has expired there is no head and the poll will return null. Expiration occurs when an element’s getDelay(TimeUnit.NANOSECONDS) method returns a value less than or equal to zero. Even though unexpired elements cannot be removed using take or poll, they are otherwise treated as normal elements. For example, the size method returns the count of both expired and unexpired elements. This queue does not permit null elements.
- Create a new DelayQueue that is initially empty. The DelayQueue class provides two constructors, one with no argument and one takes elements from another collection:
DelayQueue(Collection<? extends E> c)
- Create a DelayQueue initially containing the elements of the given collection of Delayed instances.
- Now, insert all the specified element into this delay queue using class “boolean add(E e)”.
- Retrieve and Remove an element from the DelayQueue.
- Step 1: Create a new DelayQueue that is initially empty.
- Step 2: Creates a DelayQueue initially containing elements of a collection of delayed instances.
- Step 3: Inserts the specified element into this delay queue.
- Step 4: Removes all available elements from this queue and adds them to the given collection.
- Step 5: Removes at most the given number of available elements from the queue and adds them to the given collection.
- Step 6: Inserts the specified element at the tail of this queue if possible to do so immediately without exceeding the queue’s capacity.
- Step 7: Inserts the specified element into the delay queue.
- Step 8: Retrieve but do not remove the head of this queue or simply returns null if this queue is empty.
- Step 8(a): Retrieves and removes the head of this queue or simply returns null if this queue is empty.
- Step 8(b): Retrieves and removes the head of the queue waiting if necessary until an element with an expired delay is available on this queue.
- Step 9: Insert the specified element into the delay queue.
- Step 10: Removes a single instance of the specified element from this queue, if it is present.
- Step 11: Retrieves and removes the head of this queue, waiting if necessary until an element with an expired delay is available on this queue.
Delaytimes of the DelayQueue is : 1609745787571 1609745787671 1609745787771 1609745787871 1609745787971 Element time of the DelayQueue by peeking : 1609745787571 Remaining capcity : 2147483647 DelayObject1 removed ? : true DelayQueue contains DelayObject2 ? : true hash DelayQueue contains DelayObject3 ? : true Size of the ArrayBlocingQueue : 6
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.