img
. . .
interrupted = true;
}
}
mutex.lock();
if (interrupted)
Thread.currentThread().interrupt();
}
The vast majority of programs don't deal with interrupts at all. Computational programs don't care.
Interactive programs usually are fine doing "dirty" shutdowns ["Who cares if there are open file
descriptors, sockets, etc.? System.exit() will close them and any clients can deal with it on
their end."] It's the more serious server and database programs that need to do clean shutdowns.
Cancellation State
POSIX has a more elaborate version of cancellation. It defines a cancellation state for each thread
that will enable or disable cancellation for that thread. Thus you can disable cancellation during
critical sections and reenable it afterward. Neither Win32 nor Java defines this state, although it
would not be too difficult for you to write it yourself [Implementing enableInterrupts()].
Cancellation state makes it (just barely) feasible to use asynchronous cancellation safely, although
there are still significant problems to be dealt with.
A Cancellation Example
Code Example 9-11 uses cancellation via interruption to get rid of unneeded search threads. This
program has the objective of finding a certain number by using a heuristic. The desired number is
the process ID, and the heuristic is to generate random numbers, checking to see if they happen to
be the PID (Figure 9-2). Admittedly, this is not a very clever heuristic, but the concept is solid.
You can reasonably replace the problem and heuristic with more meaningful ones, such as a chess
position and an alpha-beta search. The cancellation issues won't change.
Figure 9-2. Cancellation Example Program
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home