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

Tim Peierls tim at peierls.net
Tue Aug 23 13:31:54 EDT 2005


Peter Veentjer - Anchor Men wrote:
> Let me explain where this NullWaiter is going to be part of. I need 
> something that can be compared to a ThreadPoolExecutor. But I don`t
> want to execute a single job, but I want to execute a job by all
> threads in that structure repeatedly (untill the task is set to null). I have
> called this structure the Repeater. The NullWaiter is going to be
> part of the Repeater and contains the task all threads are waiting for.
> If no task is available, all threads wait. But if a task is available,
> they will keep repeating it..over.. and over.. and over.

I still don't really understand the use case, but does this interface describe 
what you want?

   interface AwaitableValue<V> {
       V get() throws InterruptedException;
       void set(V value);
   }

And if so, does this simpler implementation (doesn't use RWL) meet your needs?

   class SimpleAwaitableValue<V> implements AwaitableValue<V> {
       private volatile V value;
       public V get() throws InterruptedException {
           V v = value;
           if (v != null) return v;
           synchronized (this) {
               while (value == null) wait();
               return value;
           }
       }
       public synchronized void set(V v) {
           value = v;
           if (value != null) notifyAll();
       }
   }

--tim



More information about the Concurrency-interest mailing list