Re: Making one or more threads wait for another to produce a value
or fail
On 5/31/2011 7:00 AM, Tom Anderson wrote:
The scenario:
Penelope is a widow, or at least her husband isn't around any more
(she's not sure which; long story). There are 108 suitors who would like
to marry her. She hasn't decided which one she'll marry. So, the 108
suitors are sitting about waiting for her to decide. It's possible that
instead of deciding to marry one of them, she'll deliver some other,
exceptional, verdict (eg "turns out my husband is still alive, and will
now murder you all").
Just popping in quickly. First, everyone's all about CountDownLatch.
Does this really work? It requires a fixed number of threads supplied
on creation to "fire," iirc.
Second what about other java.util.concurrent classes? Both locks (esp
ReadWriteLock) and AbstractQueuedSynchronizer look promising.
<http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/AbstractQueuedSynchronizer.html>
Especially the second example for AQS, which is "a latch class that is
like a CountDownLatch except that it only requires a single signal to
fire. Because a latch is non-exclusive, it uses the shared acquire and
release methods."
Lastly, I must be dense, I have a simple implementation but I think it's
wrong. What is wrong with it? I'm sure I've missed something in the
original specification, but I'm drawing a blank. How is the exceptional
verdict delivered? There's also no way to set a verdict for one thread
vs setting a return value for all threads. I think your initial
specification of using just "await()" and "deliver()" might be a bit too
simple for the problem.
This is just a simple class that sends a single message (the verdict) to
all threads (i.e., it broadcasts the message). Note this is untested.
public class BroadcastSynchronizer<V, E extends Throwable> {
private volatile V verdict;
private final Object lock = new Object();
public V await() throws E, InterruptedException {
while( verdict == null ) {
synchronized( lock ) {
if( verdict == null )
lock.wait();
}
}
return verdict;
}
public void deliver( V verdict ) {
this.verdict = verdict;
synchronized( lock ) {
lock.notifyAll();
}
}
}