Understanding Java Delayed Queue Pitfall

Bhuvan Gupta
1 min readDec 30, 2018

--

Java provides the Delay Queue which says

An unbounded blocking queue of delayed elements, in which an element can only be taken when its delay has expired

But It hangs and may not return the elements.

In Nice Happy world

Here, the first poll waits for 10 mills and then returns the value and then the second poll wait for 10Sec and then return the value.

EveryThing is as Expected :)

During 2 AM in the morning.

Here expectation is that the first poll will return after waiting for 10 mills and the second poll will return after 10-sec delay.

BUT THAT’s NOT THE CASE. the first poll will return after waiting for 10-sec and the second poll will return after 10milli delay.

Note: Some people will say that it is maintaining the queue behaviour of FIFO. But if we add let say 1 sec wait between offer and poll the behaviour changes to printing after10mill first and after10sec later.

So if one adds a LazyMsg with a delay of 10 Day. All msgs are getting delayed for 10 days :(

Motivation of the article: Random thoughts on how timer works in Java

--

--