Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18924
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Interrupted exception chaining |
| Date | 2012-09-25 23:23 +0200 |
| Organization | albasani.net |
| Message-ID | <k3t7bn$meb$1@news.albasani.net> (permalink) |
| References | (1 earlier) <ETk8s.3$kz7.0@newsfe02.iad> <k3t429$f84$1@news.albasani.net> <k3t4lq$tmk$1@dont-email.me> <k3t59s$i1e$1@news.albasani.net> <k3t6gk$b8o$1@dont-email.me> |
markspace schrieb:
>> public boolean done; /* should be set by the thread that
>> notifies the lock */
>
>
> While I agree this works in some cases,
Just replace done by the <cond> you want to anyway check.
The main point is that you cannot go, since factoring
out the programming pattern works not:
synchronized (c) {
while (!<cond>)
uninterruptableWait(c);
}
But rather simply apply the programming pattern:
try {
synchronized (c) {
while (!<cond>)
c.wait();
}
} catch( InterruptedException ex ) {
Thread.currentThread().interrupt();
}
Or if you want to scare the hell out of your clients, use:
public interface Predicate {
public boolean _true(Object c);
}
public void uninterruptableWait(Object c, Predicate p) {
try {
synchronized (c) {
while (!p._true(c))
c.wait();
}
} catch( InterruptedException ex ) {
Thread.currentThread().interrupt();
}
}
Then what you call "SpinLock", but what I would call
"OneTimeLock". Can be implemented as follows:
public void waitDone() {
uninterruptableWait(lock,new Predicate() {
return done;
});
}
Of course you can turn a "OneTimeLock" into a "ManyTimeLock",
for example. You can reset the done inside the synchronized
of the waitDone(). The synchronized will assure that when
you leave the synchronized the done=false holds, since no
other thread will interfer while inside the synchronized
and after the wait():
public void waitDone() {
try {
synchronized( lock ) {
while( !done )
lock.wait();
done=false;
}
} catch( InterruptedException ex ) {
Thread.currentThread().interrupt();
}
}
To abstract this pattern we would need:
public interface Predicate {
public boolean _true(Object c);
}
public interface Action{
public boolean perform(Object c);
}
public void uninterruptableWait(Object c, Predicate p, Action a) {
try {
synchronized (c) {
while (!p._true(c))
c.wait();
a.perform(c);
}
} catch( InterruptedException ex ) {
Thread.currentThread().interrupt();
}
}
And one can then do:
public void waitDone() {
uninterruptableWait(lock,new Predicate() {
return done;
}, new Action() {
done=false;
});
}
Eagerly avaiting JDK 7 lambdas...
Bye
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Interrupted exception chaining raphfrk@gmail.com - 2012-09-25 03:25 -0700
Re: Interrupted exception chaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-25 08:56 -0400
Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 08:33 -0700
Re: Interrupted exception chaining Ivan Ryan <ivan.ryan@gmail.com> - 2012-09-25 09:28 -0700
Re: Interrupted exception chaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-25 13:12 -0400
Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 12:00 -0700
Re: Interrupted exception chaining Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-25 09:22 -0700
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:26 +0200
Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 13:37 -0700
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:47 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:53 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:53 +0200
Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 14:08 -0700
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:23 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:24 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:32 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:42 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:48 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:55 +0200
Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 15:33 -0700
Re: Interrupted exception chaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-25 21:43 -0400
Re: Interrupted exception chaining Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-26 01:43 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-26 10:24 +0200
Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-26 10:32 +0200
Re: Interrupted exception chaining Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-26 16:26 +0200
csiph-web