Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 31 May 2011 11:06:06 -0500 Date: Tue, 31 May 2011 09:06:00 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Making one or more threads wait for another to produce a value or fail References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 59 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-ZarE/DrBW5uuGB0unLun73qyMKLwcJYLons/yoUAhL23/PstGtfvL2OI5c39VD0wTVtgXf5kABsa2G7!Acfsk58hnqdpSnBpvUjw31E3hlw/XxPumNaiasi2e1mr1gvoOfqxIWajLkUAVxvtTgsuTkcLSThO!tmQ0Jkdd5m0AXYml94KOx27hfHXeqshUVezkaiVYFls= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3247 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4801 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"). > > Penelope is a thread, as are her suitors. Penelope has plenty to do > after she delivers her verdict, so the verdict is not a return value - > it's a value she'll pass to a method. In code, this looks something like: > > class Penelope implements Runnable { > public void run() { > try { > Verdict v = ... ; > DELIVER(v); > } > catch (Exception e) { > DELIVER_EXCEPTION(e); > } > } > } > > class Suitor implements Runnable() { > public void run() { > try { > Verdict v = AWAIT(); > } > catch (Exception e) { > // alas > } > } > } > > There has got to be something in java.util.concurrent that she can use > to deliver her verdict. What? > > In terms of synchronisation, CountdownLatch with a count of 1 does it - > the suitors await, and Penelope counts down. But it has no way to pass a > value. In any case, I would wrap the verdict delivery issues up in a class so that neither Penelope nor the suitors need to know about the synchronization implementation. Within that class, I would probably use ordinary fields to represent the verdict, exceptional or otherwise. One way to do the synchronization would be a semaphore that is initially zero, but with a large number of permits added when Penelope calls a setVerdict method. The getVerdict method that the suitors call would wait to get a permit, record the verdict, and put the permit back so there is no possibility of running out of permits. Patricia