<<< Thread interruption - InterruptedException | Index | Example: interrupting a thread >>> |
Interrupts are very effective way to cancel tasks
If the interrupted method is not calling a blocking method like like sleep() or wait(), then
interrupt() will not automatically interrupt the thread
To manually check for the interrupted flag, the thread should call isInterrupted():
try { while ( true ) { // do some work... if ( Thread.interrupted() ) { throw new InterruptedException(); } } } catch ( InterruptedException iex ) { // We've been interrupted: Thread.currentThread().interrupt(); return; }
<<< Thread interruption - InterruptedException | Index | Example: interrupting a thread >>> |