J
Jan Burse
Dear All
I am checking how I could code fully cancelable
blocking methods. i.e. they should cancel when
I call interrupt once.
Assume I have a method as follows:
void bla() {
synchronized(lock) {
A;
while (!cond) {
wait();
}
B;
}
}
I could simply swallow the InterruptedException from
wait. Now if I catch it, I can rethrow it or set the
interrupt flag again via interrupted. So my first idea
was as follows:
void bla() {
synchronized(lock) {
A;
while (!cond) {
try {
wait();
} catch (InterruptedException x) {
C; /* some clean up */
Thread.currentThread().interrupt();
return;
}
}
B;
}
}
But my conclusion so far that this is not fully working.
Since during A, cond and B also the interrupt flag could
be set, and if I don't check it, I will enter wait and
block infinitely, although interrupt was called once.
void bla() {
synchronized(lock) throws InterruptedException {
A;
while (!cond && !Thread.currentThread().isInterrupted) {
try {
wait(1000);
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
}
}
if (Thread.currentThread.interrupted()) {
C; /* some cleanup */
throw new InterruptedException();
}
B;
}
}
So I need to use a wait() with a timeout parameter, otherwise
the interruption might get unnoticed. Right?
Bye
I am checking how I could code fully cancelable
blocking methods. i.e. they should cancel when
I call interrupt once.
Assume I have a method as follows:
void bla() {
synchronized(lock) {
A;
while (!cond) {
wait();
}
B;
}
}
I could simply swallow the InterruptedException from
wait. Now if I catch it, I can rethrow it or set the
interrupt flag again via interrupted. So my first idea
was as follows:
void bla() {
synchronized(lock) {
A;
while (!cond) {
try {
wait();
} catch (InterruptedException x) {
C; /* some clean up */
Thread.currentThread().interrupt();
return;
}
}
B;
}
}
But my conclusion so far that this is not fully working.
Since during A, cond and B also the interrupt flag could
be set, and if I don't check it, I will enter wait and
block infinitely, although interrupt was called once.
void bla() {
synchronized(lock) throws InterruptedException {
A;
while (!cond && !Thread.currentThread().isInterrupted) {
try {
wait(1000);
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
}
}
if (Thread.currentThread.interrupted()) {
C; /* some cleanup */
throw new InterruptedException();
}
B;
}
}
So I need to use a wait() with a timeout parameter, otherwise
the interruption might get unnoticed. Right?
Bye