Open In App

Java Program to Implement DelayQueue API

Last Updated : 21 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Procedure: 

  • 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()

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.

Implementation:

  • 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.

Example:


Output
Delaytimes of the DelayQueue is           : 1626870778483    1626870778583    1626870778683    1626870778786    1626870778886    
Element time of the DelayQueue by peeking :  1626870778483
Remaining capacity                         : 2147483647
DelayObject1 removed ?                    : true
DelayQueue contains DelayObject2 ?        : true
hash DelayQueue contains DelayObject3 ?   : true
Size of the ArrayBlocingQueue             : 6

 
 


 


Next Article

Similar Reads