} catch (InterruptedException ie) {
// Impossible
synchronized (t) {
t.inCriticalSection = false; // Now it's OK to cancel
// The Interrupter Thread
synchronized (t) {
while (t.inCriticalSection) {
try  {
} catch (InterruptedException ie) {
// Impossible
Exit on interrupt()
You could install interruption handlers that will exit the thread right there and then, assuming that
the programmer always intends interruption to be cancellation and that all data is consistent
anytime your library is called. This is also too much for a library to assume. Don't do that.
Propagate InterruptedException
You could propagate the exception. By propagating, you shift the burden of dealing with the
exception to the callers, who must then treat your libraries as throwing
InterruptedException. Propagating the exception is certainly the right thing to do for many
library functions.
The value of this is that if your library makes unbounded blocking calls, anyone who used it
would want to be able to interrupt it. If you make bounded-time blocking calls, it's not so vital. It's
generally OK if your library call takes 40 ms and you don't interrupt it.
You may wish to avoid dealing with InterruptedException at all. In such cases you can set
a flag, reenter whatever code you were running, wait until that code returns normally, and then
call interrupt on yourself before leaving.
The point here is that (1) you don't want your code to do anything with interrupts at all, (2) you
don't want the caller to have to deal with InterruptedException being thrown from your
code, and (3) you really wish that you could have called a method that didn't throw that exception
at all, but there was no alternative. This is a common thing to do.
The mutex class shown in Code Example 9-7 (this is the actual code we use) exemplifies this
situation. In this code we don't want to be bothered with interrupts and we don't want the caller of
mutex.lock() to be forced to catch InterruptedException all the time. So we simply call
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks