mutex.unlock();
wait();
}
} catch (InterruptedException ie) {
// NB: There is no 'while' loop
interrupted = true;
}
mutex.lock();
if (interrupted)
Thread.currentThread().interrupt();
}
public void readLock() {
m.lock();
nWaitingReaders++;
while ((owner != null) || (nWaitingWriters > 0)) {
readersCV.condWait(m);
}
nWaitingReaders--;
nCurrentReaders++;
m.unlock();
}
Example 9-9 Handling Interruptions from condWait() the Hard Way
public void readLock() {
booleaninterrupted=false;
m.lock();
nWaitingReaders++;
while ((owner != null) || (nWaitingWriters > 0)) {
if (Thread.interrupted())
interrupted = true;
readersCV.condWait(m);
}
nWaitingReaders--;
nCurrentReaders++;
m.unlock();
if (interrupted)
Thread.currentThread().interrupt();
}
Example 9-10 The Right Way of Implementing condWait()
public void condWait(Mutex mutex, long timeout) {
boolean interrupted = false;
while (true) {
try {
synchronized (this) {
mutex.unlock();
wait(timeout);
break;
}
} catch (InterruptedException ie) {
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home