Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.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:25:58 -0700 Organization: A noiseless patient Spider Lines: 55 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:26:09 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="fblHIPffyJ67C5r/ar9cfA"; logging-data="11896"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zA0PyBr/79vQfw+odxMTQtB7WGSBcQjY=" 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:LgFCY/dt0kl6Jyu3mwB7m/J+IVA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4904 Try #2. I've added "deliverOne()" and "deliverAll()" to distinguish between a message sent to a single thread, and a message sent to all threads. However, this doesn't seem to meet your requirement for an exception. So I've added also "deliverException()" which is basically the same as "deliverAll()" except with the semantic that the receiver sees an exception thrown rather than seeing a return value. Again this is untested. Batter running low! Back later! package test; public class BroadcastSynchronizer { private volatile E exception; private volatile V verdict; private final Object lock = new Object(); public V await() throws E, InterruptedException { while( verdict == null && exception == null ) { synchronized( lock ) { if( verdict == null && exception == null ) lock.wait(); } } if( exception != null ) { throw exception; } return verdict; } public void deliverAll( V verdict ) { this.verdict = verdict; synchronized( lock ) { lock.notifyAll(); } } public void deliverOne( V verdict ) { this.verdict = verdict; synchronized( lock ) { lock.notify(); } } public void deliverException( E exception ) { this.exception = exception; synchronized( lock ) { lock.notifyAll(); } } }