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

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


A Future is meant to be used a single time. My structure needs to be updated null/value/value/null/value etc many many times and threads waiting for a value need to sleep if there is no value. 

You can`t unset a Future.. My structure needs to be set/unset many times.  

-----Oorspronkelijk bericht-----
Van: Michael Hicks [mailto:mwh at cs.umd.edu] 
Verzonden: dinsdag 23 augustus 2005 14:08
Aan: Peter Veentjer - Anchor Men
Onderwerp: Re: [concurrency-interest] Waiting for object value to be available.

Looks similar to a Future, and you might be able to use a FutureTask instead of your code below, depending on how you want to implement creation of the stored value; see http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
Future.html.

Mike

On Aug 23, 2005, at 8:00 AM, 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)
>  
> 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