[concurrency-interest] Waiting for object value to be available.

Doug Lea dl at cs.oswego.edu
Tue Aug 23 12:02:45 EDT 2005

Peter Veentjer - Anchor Men wrote:
> I have the following problem.
> I want some kind of container that can store an object. Multiple threads 
> can read a value from that container, and if there is no object 
> available (the value is null) they are going to sleep (or sleep untill a 
> timeout occurs). If there is a value available, all threads that where 
> sleeping wake up.. and read the value (but don`t remove it like a in 
> blockingqueue)

I think the most common approach to this sort of problem is to use
an Observer or Listener design. (My CPJ book includes a description
in section 3.5.2). This might be overkill here if you don't want or
need to otherwise maintain observers in lists and multicast to them
change notifications. If so, you might be able to make a simple
custom synchronization scheme. One possibility is to associate a version
number with your data objects, and have the observer threads wait
for versions to change from their last known version, and signalling
all waiters when they do change. Note that in these kinds of schemes
though, observers might miss seeing one of the versions. If that's not
OK, you probably need a full listener/multicast design to allow
tighter coordination.


More information about the Concurrency-interest mailing list