File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Precision of BlockingQueue.poll timeout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Precision of BlockingQueue.poll timeout?" Watch "Precision of BlockingQueue.poll timeout?" New topic

Precision of BlockingQueue.poll timeout?

Mario Zagar

Joined: Feb 18, 2010
Posts: 1
Hello all,

I am experiencing unusually long waits before timeout when using BlockingQueue<T>.poll(long timeout, TimeUnit unit).

I would expect poll() to timeout within operating system timer precision error (e.g. 15ms on Windows).

According to my tests poll() waits alot longer.
For example, if timeout is set to 2000ms, poll() timeout occurrs after ~3200ms and not after (as I would expect) ~2015ms.

I do not understand why the precision of BlockingQueue.poll() timeout is so poor.

If I cannot rely on poll() to wait "up to" (as stated in javadoc) specified timeout, I do not see the practical use for this method.
(or should "up to" be understood as "never less than but can be extremly over specified timeout")

Could anyone please shed some light on this matter and maybe point me in the right direction as to what am I doing wrong ?
Is my understanding of what poll() should be used for and when wrong ?

Here is my test code which measures how long did poll() wait before timing out & the resulting output.
(Windows XP SP3, jre

Resulting output:

i=0, poll timeout after: 2422
i=1, poll timeout after: 3156
i=2, poll timeout after: 3219
i=3, poll timeout after: 3281
i=4, poll timeout after: 3203
i=5, poll timeout after: 2829
i=6, poll timeout after: 2843
i=7, poll timeout after: 3282
i=8, poll timeout after: 3125
i=9, poll timeout after: 3250


I agree. Here's the link:
subject: Precision of BlockingQueue.poll timeout?
jQuery in Action, 3rd edition