Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Making one or more threads wait for another to produce a value or fail Date: Thu, 02 Jun 2011 14:10:28 -0700 Organization: A noiseless patient Spider Lines: 64 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 2 Jun 2011 21:10:39 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="fblHIPffyJ67C5r/ar9cfA"; logging-data="5751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186N8eVtsAaJitgRjMdpDtu4LZhRVD+UBc=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:TLelXwi31RWwT4gBw3rR7UCulI4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4902 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. 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 { 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(); } } }