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

Joe Bowbeer joe.bowbeer at gmail.com
Tue Aug 23 18:32:01 EDT 2005


Oops.  Scratch the ArrayBlockingQueue idea.  poll is not peek and vice versa..

I still think the BlockingQueue interface is a reasonable model, but
you'd need a custom implementation.

Back to resettable Future then.


On 8/23/05, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:
> I suggest a custom Future implementation or a custom BlockingQueue
> implementation.
> 
> As others have pointed out, Future provides the desired get() or
> get(timeout) API for the reading client.
> 
> Similary, BlockingQueue provides poll() and poll(timeout).
> 
> In terms of implementation, these approaches resolve to either
> creating a resetable FutureTask or employing a single-entry
> BlockingQueue (where the writing client would first take the only
> element from the queue and then put a new element).
> 
> I suggest the latter.  You can repackage the API to hide the
> BlockingQueue 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();
> >   }
> >  } */
> > }
> >



More information about the Concurrency-interest mailing list