Two-Phase Termination

Origin: Grand 98
Reason: To orderly shutdown a thread.
Synopsis: Set a latch that a thread checks at strategic points of its execution.
Example: A server that assigns a thread to each client must handle requests to terminate a client's thread or its own execution.
Solution: The typical run method of a thread should look like
public void run () {
    initialize ();
    while (! isInterrupted ()) {
        ... // whatever execution
    }
    shutdown ();
}
Method isInterrupted tests the interrupted status of a thread. The interrupted status is set when a thread's method interrupt is called and is cleared in various situations.
See also: