Methods Summary |
---|
final java.lang.Object | getCurrentState()Returns current state.
synchronized (this) {
return currentState;
}
|
final void | interruptWait()Interrupts waiters
synchronized (this) {
interruptWait = true;
notifyAll();
}
|
protected boolean | isStateReached(java.lang.Object state)Checks condition waiters are waiting for:
specified state has been reached.
Assumption: 'this' lock is already taken.
return (currentState == state);
|
final void | setCurrentState(java.lang.Object state, boolean interrupt)Sets a state.
Assumption: only one thread sets a state,
'this' lock shouldn't be already taken.
synchronized (this) {
if (currentState == state) {
return;
}
interruptWait = interrupt;
currentState = state;
waitersToWake = totalWaiters;
notifyAll();
}
// wait until all waiters have been awaken, so they have
// a chance to recheck condition they are waiting for
synchronized (wakeWaitersLock) {
while (waitersToWake != 0) {
try {
wakeWaitersLock.wait();
} catch (InterruptedException e) {
// just ignore
}
}
}
|
final void | waitFor(java.lang.Object state)Waits until specified state has been reached.
synchronized (this) {
while (!isStateReached(state) && !interruptWait) {
try {
totalWaiters++;
wait();
totalWaiters--;
synchronized (wakeWaitersLock) {
waitersToWake--;
// if all threads affected by setCurrentState
// notification have waken up, notify setCurrentState
// about it
if (waitersToWake == 0) {
wakeWaitersLock.notify();
}
}
} catch (InterruptedException e) {
// just ignore
}
}
}
|