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

Peter Veentjer - Anchor Men p.veentjer at anchormen.nl
Tue Aug 23 13:39:27 EDT 2005


I still don't really understand the use case, but does this interface describe
what you want?
 
--------------------------------------
 
Yes it does.. I allready had removed the read write lock but your name is a lot better than mine :)
 
public class Bar<E> {
 private final ReentrantLock _lock;
 private volatile E _item;
 private final Condition _availableCond;
 public Bar(boolean fair){
  _lock = new ReentrantLock(fair);
  _availableCond = _lock.newCondition();
 }
 public E get() throws InterruptedException {
  _lock.lockInterruptibly();
  try{
   while(_item == null)
    _availableCond.wait();
   return _item;
  }finally{
   _lock.unlock();
  }
 }
 public void set(E item) throws InterruptedException {
  _lock.lockInterruptibly();
  try{
   _item = item;
   if(_item!=null)
    _availableCond.signalAll();
  }finally{
   _lock.unlock();
  }
 }
}

 
But I`m going to need the read write lock. I don`t want multiple values of the WaitableValue out there.. only one is allowed (I only want one task-instance running, not two).. So I`m going to use the read write lock to garantuee that all old values are returned before a new value is available.




More information about the Concurrency-interest mailing list