Tuesday, July 21, 2015

DelayQueue class and Delayed interface example in Java

In this article, we look into one of the implementations in java.util.concurrent support the extended BlockingQueue interface, that defines blocking versions of put and take i.e, DelayQueue.  An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. We'll try to understand the concept of DelayQueue class and Delayed interface with the help of example.



What is DelayQueue?
An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. In this class, you can store elements with an activation date. The methods that return or extract elements of the queue will ignore those elements whose data is in the future. They are invisible to those methods. Here Delayed elements are those elements who implements Delayed interface.


What is Delayed Interface?
A mix-in style interface for marking objects that should be acted upon after a given delay.

This interface allows you to work with delayed objects, so you will implement the activation date of the objects stored in the DelayedQueue class as the time remaining until the activation date. This interface forces to implement the following two methods

  • compareTo(Delayed o): The Delayed interface extends the Comparable interface. This method will return a value less than zero if the object that is executing the method has a delay smaller than the object passed as a parameter, a value greater than zero if  the object that is executing the method has a delay bigger than the object passed as a parameter, and the zero value if both objects have the same delay.
  •  getDelay(TimeUnit unit): This method has to return the time remaining until the activation date in the units is specified by the unit parameter. The TimeUnit class is an enumeration with the following constants: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, and SECONDS.

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 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. This queue does not permit null elements.


Example
In this example, you will learn how to use the DelayedQueue class storing in it some events with different activation dates

we have implemented the Event class. That class has a unique attribute, the activation date of the events, and implements the Delayed interface, so you can store Event objects in the DelayedQueue class.



The getDelay() method returns the number of nanoseconds between the activation date and the actual date. Both dates are objects of the Date class. You have used the getTime() method that returns a date converted to milliseconds and then, you have converted that value to TimeUnit received as a parameter. The DelayedQueue class works in nanoseconds, but at this point, it's transparent to you.

The compareTo() method returns a value less than zero if the delay of the object executing the method is smaller than the delay of the object passed as a parameter, a value greater than zero if the delay of the object executing the method is bigger than the delay of the object passes as a parameter, and the value 0 if both delays are equal.


This class has an integer attribute named id. When a Task object is executed, it adds a number of seconds equal to the ID of the task to the actual date and that is the activation date of the events stored by this task in the DelayedQueue class. Each Task object stores 10 events in the queue using the add() method.

Finally, in the main() method of the Main class, you have created sixTask objects and executed them in the corresponding threads. When those threads finish their execution, you have written to the console all the events using the poll() method. That method retrieves and removes the first element of the queue. If the queue does not have any active element, the method returns the null value. You called the poll() method and if it returns an Event class, you increment a counter. When the poll() method returns the null value, you write the value of the counter in the console and put the thread to sleep during half a second to wait for more active events. When you have obtained the 500 events stored in the queue, the execution of the program finishes


You can see how the program only gets 10 events when it is activated.

The DelayQueue class has other interesting methods, which are as follows:

  • clear(): This method removes all the elements of the queue.
  • offer(E e): E represents the class used to parameterize the DelayQueue class. This method inserts the element passed as a parameter in the queue.
  • peek(): This method retrieves, but doesn't remove the first element of the queue.
  • take(): This method retrieves and removes the first element of the queue. If there aren't any active elements in the queue, the thread that is executing the method will be blocked until the thread has some active elements.



If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

3 comments:

  1. great java tips At SynergisticIT we offer the best java bootcamp

    ReplyDelete
  2. The article is very informative and interesting for those who are interested in blogging. Great article! I enjoyed reading it very much. You can find my profile on my profile page. The faster you type on this profile, the faster you will be able to communicate with others. With our free typing speed test, you can check your WPM and typing scores in seconds.

    ReplyDelete