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

Joe Bowbeer joe.bowbeer at gmail.com
Tue Aug 23 17:52:44 EDT 2005


I suggest a custom Future implementation or a custom BlockingQueueimplementation.
As others have pointed out, Future provides the desired get() orget(timeout) API for the reading client.
Similary, BlockingQueue provides poll() and poll(timeout).
In terms of implementation, these approaches resolve to eithercreating a resetable FutureTask or employing a single-entryBlockingQueue (where the writing client would first take the onlyelement from the queue and then put a new element).
I suggest the latter.  You can repackage the API to hide theBlockingQueue details from the user, but the details would look like:
queue = new ArrayBlockingQueue<E>(1);
// readers
E = queue.poll(timeout, unit);
// writer
queue.clear();queue.put(E);

Joe.


On 8/23/05, Peter Veentjer - Anchor Men <p.veentjer at anchormen.nl> 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) >   > The problem: > -A good name for such a structure. At the moment I have NotNullWaiter, but> well.. that sucks. > -I want to know if there exist something like it. >   > And I would like it if you would have a look at my code. >   > public class NotNullWaiter<E> { >   >  private final ReadWriteLock _lock;>  private final Condition _itemAvailable;>  private volatile E _item; >   >  public NotNullWaiter(){>   this(false);>  } >   >  public NotNullWaiter(boolean fair){>   _lock = new ReentrantReadWriteLock(fair);>   _itemAvailable = _lock.writeLock().newCondition();>  } >   >  public E read()throws InterruptedException{>   _lock.readLock().lockInterruptibly(); >   >   try{>    while(_item==null)>     _itemAvailable.wait(); >   >    return _item;>   }finally{>    _lock.readLock().unlock();>   }>  } >   >  public E read(long timeout, TimeUnit timeUnit)throws InterruptedException{>   if(timeUnit == null)throw new NullPointerException(); >   >   _lock.readLock().lockInterruptibly(); >   >   try{>    long nanos = timeUnit.toNanos(timeout);>    for (;;) {>     //if there is an item available, return it.>     if (_item !=null)>      return _item; >   >     //if the time has passed, return null to indicate no item is available.>     if (nanos <= 0)>      return null; >   >     try {>      nanos = _itemAvailable.awaitNanos(nanos);>     } catch (InterruptedException ie) {>      _itemAvailable.signal(); // propagate to non-interrupted thread (todo:> why???)>      throw ie;>     }>    }>   }finally{>    _lock.readLock().unlock();>   }>  } >   >  public void write(E item)throws InterruptedException{>      _lock.writeLock().lockInterruptibly(); >   >   try{>    _item = item;>    if(item != null)>     _itemAvailable.signalAll();>   }finally{>    _lock.writeLock().unlock();>   }>  } >   >  /*>  private void write(E e, long timeout, TimeUnit timeUnit)throws> InterruptedException{>     if(timeUnit == null)throw new NullPointerException(); >   >   _lock.writeLock().lockInterruptibly();>   try{>       long nanos = timeUnit.toNanos(timeout);>             for (;;) { >   >    }>   }finally{>    _lock.writeLock().unlock();>   }>  } */> }>  >  > > Met vriendelijke groet,> > Peter Veentjer> Anchor Men Interactive Solutions – duidelijk in zakelijke> internetoplossingen> > Praediniussingel 41> 9711 AE Groningen> > T: 050-3115222> F: 050-5891696> E: p.veentjer at anchormen.nl> I : www.anchormen.nl >   >  > > Met vriendelijke groet,> > Peter Veentjer> Anchor Men Interactive Solutions – duidelijk in zakelijke> internetoplossingen> > Praediniussingel 41> 9711 AE Groningen> > T: 050-3115222> F: 050-5891696> E: p.veentjer at anchormen.nl> I : www.anchormen.nl >   > _______________________________________________> Concurrency-interest mailing list> Concurrency-interest at altair.cs.oswego.edu> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest> > >


More information about the Concurrency-interest mailing list