I am curious whether it is possible to pause a thread t
in Java and allow another thread to resume it later, by having t
run the following pause code:
while(true) {
try {
synchronized(t) {
t.wait();
}
} catch(InterruptedException e) {
break;
}
}
And then resuming the thread t
by calling .interrupt()
on it. However, I have read about spurious wake-ups, and so I wondered whether my code can fail, in the sense of exiting the while-loop despite no other thread calling .interrupt()
on it. While this answer and this answer state that there are no spurious interrupts, and hence my code will never fail, the Java docs does not seem to address this. My question probably boils down to whether InterruptedException
is ever thrown without the thread being interrupted by .interrupt()
. Is there any official source or documentation that confirms this?