[concurrency-interest] Use of blocked queues

Doug Lea dl at cs.oswego.edu
Mon May 30 08:27:29 EDT 2005


Calum MacLean wrote:
> Hi
> 
> I'm wanting to use a blocked queue.
> 
> In my particular usage, when adding to the queue, I want to check if there 
> are any readers which are blocked, waiting for something to arrive on the 
> queue.  This is so that, if no readers are waiting, I fire off an event.
> 
> Is this possible using any of the java.util.concurrency blocking queues? I 
> currently can't see a way to do this using the existing classes.  I want to 
> avoid reinventing the wheel if necessary!
> 

First, be aware that this sort of design has an intrinsic race.
Even if it looks like there are no threads waiting when you
check, by the time you issue the event, there may be some.
So your overall design must be able to accommodate false alarms.

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

-Doug


More information about the Concurrency-interest mailing list