InterruptedExecution
.
class Godot extends Thread { public void run() { while (true) { } } }
wait()
, join()
, and
sleep()
are interruptible methods.
notify()
.
synchronized
does not.
t.
interrupt()
sets thread t
's interrupt
flag to true.
t
's value is ignored.
t
is blocked, it is unblocked.
Thread.
interrupted()
returns the current
thread's interrupt flag.
t.
isInterrupted()
returns t
's interrupt
flag.
t
's flag is not changed.
interrupt()
.
class Interrupt { public boolean raised = false } final Interrupt intrpt = new Interrupt() final PCRunnable producer = new PCRunnable(intrpt) consumer = new PCRunnable(intrpt) master = new PCRunnable(intrpt)
InterruptedExecution
takes care of that.
InterruptedIOException
is essential in I-O
operations.
class PCRunnable implements Runnable private Interrupt intrpt PCRunnable(Interrupt i) intrpt = i public void run() while more work && !intrpt.raised // do more work
InterruptedExecution
s for blocking inside wait()
,
join()
, and sleep()
.
InterruptedExecution
is thrown).
class Interrupt int id public void run() while more work && intrpt.id != myId // do more work
InterruptedExecution
exceptions?
InterruptedExecution
exception.
synchronized void down() { // Manipulate some state to prepare. while (!condition) wait() // Manipulate the state some more. }
synchronized void down() // Manipulate some state in preparation. while (count < 1) try { wait() } catch (InterruptedExecution ignore) { } // Manipulate the state some more.
synchronized void down() while count < 1 try { wait() } catch (InterruptedExecution ie) // clean up throw ie
down()
could also return a boolean to indicate success or failure,
avoiding the rethrow.
class Godot extends Thread public void run() while (true) int i = 1000 while (i-- > 0) compute() if (Thread.interrupted()) // clean up and exit
This page last modified on 14 July 2003.