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

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


I think the most common approach to this sort of problem is to use
an Observer or Listener design. (My CPJ book includes a description
in section 3.5.2). This might be overkill here if you don't want or
need to otherwise maintain observers in lists and multicast to them
change notifications.
------------------------
It is very important that the threads that want to read the value, are 
going to sleep untill a value is available. I think it is more complex 
to create this behaviour by using a observer/observable than a
small concurrency stone.
 
And you could see the wait/notify functionality of java as a
observer/observable. The observers (threads) are waiting in a list (every java object
can have a collection of threads waiting to be notified) and the event 
would be the signal. So I don`t see the need to create java objects for 
something that already is in the core of Java.
 
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.
 
Why do I need such a strange beast? It is going to be part
of a channels project I`m writing. One of the usages is that a
Repeater is going to 'suck' messages out of a channel.
 
example:
 
 public static void main(String[] args){
  Channel<FetchRequest> requestChannel = new StdBufferedChannel<FetchRequest>(500);
  Channel<FetchResult> resultChannel = new StdBufferedChannel<FetchResult>(50);
  
  RepeaterService fetchRepeater = new StdRepeaterService(5);//5 threads
  fetchRepeater.start();
  Fetcher fetcher = new Fetcher(requestChannel,resultChannel,fetchRepeater);
  
  RepeaterService analyzeRepeater = new StdRepeaterService(1);//1 thread
  analyzeRepeater.start();
  Analyzer analyzer = new Analyzer(resultChannel,requestChannel,analyzerExecutor);
  
  try {
       requestChannel.put(new FetchRequest(new URL("http://dmoz.org/")));
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
 }
 
 
 
 





More information about the Concurrency-interest mailing list