img
. . . .
We solved this dilemma in our code for sleep() by writing a method
InterruptibleThread.sleep() which simply caught InterruptedException and then
interrupted the thread again as it exited. The same general technique can be used for wait() or
any other method that throws InterruptedException (Code Example 7-13).
Example 7-13 Ignoring InterruptedException
public static void sleep(long time)
{
boolean interrupted = false;
try {
Thread.sleep(time);
} catch (InterruptedException ie) {
interrupted = true; // Forget timeout
}
if (interrupted)
Thread.currentThread().interrupt();
}
This technique is nice when you don't want to handle interrupts at all the places they can occur. It
is perfectly reasonable to have an interruptible program that pays attention to these interrupts only
at certain points. We'll go into greater detail in Implementing enableInterrupts().
APIs Used in This Chapter
The Class Extensions.RWLock
This is one of our classes. It implements POSIX-style readers/ writer locks. RWLocks are useful
only in very limited circumstances. Time your program carefully first!
readLock writeLock
public void readLock()
public void writeLock()
This locks the RWLock in either reader or writer mode. If a read lock is held by a different thread,
this thread will be able to get another read lock directly. If a write lock is requested, the current
thread must go to sleep, waiting for it to become available.
Chapter 7.
Reference:
unlock
public void unlock()
This unlocks the RWLock (both for readers and for writers). If this is the last reader, it will wake
up one writer thread (if any are sleeping). If this is a writer, it will wake up one writer thread (if
any are sleeping); otherwise, it will wake up all the sleeping threads with reader requests.
Chapter 7.
Reference:
The Class Extensions.Barrier
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home