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

Peter Veentjer - Anchor Men p.veentjer at anchormen.nl
Tue Aug 23 08:00:38 EDT 2005


 
 
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 <BLOCKED::mailto:p.veentjer at anchormen.nl> 
I : www.anchormen.nl <BLOCKED::blocked::http://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 <blocked::http://www.anchormen.nl/> 

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20050823/407b3f1a/attachment.htm


More information about the Concurrency-interest mailing list