[concurrency-interest] Use of blocked queues

Doug Lea dl at cs.oswego.edu
Mon May 30 08:49:46 EDT 2005

I wrote:
> Given this, from what I understand of your intent,
> I think you can do something like:
>    if (!q.offer(data)) {
>      fireEvent();
>      q.put(data);
>    }

I should have noted that this snippet is only reliable for
SynchronousQueues, which may or may not be best for you here.
If you use a queue with storage (LinkedBlockingQueue etc),
you probably want to always fire an event, because offer() cannot
distinguish queuing from taking by another thread.
While you might be able to heuristically cut down on the number of
events by first putting the item, and then checking of the queue
is still empty, this won't do a lot of good because even if
there is a waiting taker, it usually will not have completely taken the
item by the time you check.


More information about the Concurrency-interest mailing list