Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.java.programmer > #4902
| From | markspace <-@.> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Making one or more threads wait for another to produce a value or fail |
| Date | 2011-06-02 14:10 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <is8u8e$5jn$1@dont-email.me> (permalink) |
| References | <alpine.DEB.2.00.1105311443530.28134@urchin.earth.li> |
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();
}
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Making one or more threads wait for another to produce a value or fail Tom Anderson <twic@urchin.earth.li> - 2011-05-31 15:00 +0100
Re: Making one or more threads wait for another to produce a value or fail Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-05-31 07:14 -0700
Re: Making one or more threads wait for another to produce a value or fail Tom Anderson <twic@urchin.earth.li> - 2011-05-31 16:46 +0100
Re: Making one or more threads wait for another to produce a value or fail Deeyana <d.awlberg@hotmail.invalid> - 2011-05-31 19:23 +0000
Re: Making one or more threads wait for another to produce a value or fail Tom Anderson <twic@urchin.earth.li> - 2011-06-01 22:12 +0100
Re: Making one or more threads wait for another to produce a value or fail Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-05-31 20:45 -0700
Re: Making one or more threads wait for another to produce a value or fail Patricia Shanahan <pats@acm.org> - 2011-05-31 21:37 -0700
Re: Making one or more threads wait for another to produce a value or fail Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-01 07:18 -0700
Re: Making one or more threads wait for another to produce a value or fail Patricia Shanahan <pats@acm.org> - 2011-06-01 09:01 -0700
Re: Making one or more threads wait for another to produce a value or fail Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 16:40 +1200
Re: Making one or more threads wait for another to produce a value or fail "John B. Matthews" <nospam@nospam.invalid> - 2011-06-01 11:22 -0400
Re: Making one or more threads wait for another to produce a value or fail Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-02 10:15 +1200
Re: Making one or more threads wait for another to produce a value or fail "John B. Matthews" <nospam@nospam.invalid> - 2011-06-01 21:38 -0400
Re: Making one or more threads wait for another to produce a value or fail Tom Anderson <twic@urchin.earth.li> - 2011-06-01 22:07 +0100
Re: Making one or more threads wait for another to produce a value or fail Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-01 22:46 -0700
Re: Making one or more threads wait for another to produce a value or fail markspace <-@.> - 2011-05-31 08:26 -0700
Re: Making one or more threads wait for another to produce a value or fail Tom Anderson <twic@urchin.earth.li> - 2011-05-31 19:13 +0100
Re: Making one or more threads wait for another to produce a value or fail Patricia Shanahan <pats@acm.org> - 2011-05-31 09:06 -0700
Re: Making one or more threads wait for another to produce a value or fail Paul Cager <paul.cager@googlemail.com> - 2011-05-31 10:08 -0700
Re: Making one or more threads wait for another to produce a value or fail Patricia Shanahan <pats@acm.org> - 2011-05-31 10:39 -0700
Re: Making one or more threads wait for another to produce a value or fail Paul Cager <paul.cager@googlemail.com> - 2011-05-31 16:24 -0700
Re: Making one or more threads wait for another to produce a value or fail Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 12:33 +1200
Re: Making one or more threads wait for another to produce a value or fail "John B. Matthews" <nospam@nospam.invalid> - 2011-06-01 00:38 -0400
Re: Making one or more threads wait for another to produce a value or fail markspace <-@.> - 2011-06-02 14:10 -0700
Re: Making one or more threads wait for another to produce a value or fail markspace <-@.> - 2011-06-02 14:25 -0700
Re: Making one or more threads wait for another to produce a value or fail Patricia Shanahan <pats@acm.org> - 2011-06-02 14:43 -0700
csiph-web