Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Avoid notifying when no one is waiting on proceedLock #1657

Merged
merged 4 commits into from
Jan 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ protected void doWait() {
// or no wakeUp() is called during the thread's run, will the thread wait().
if (nextTimeoutIntervalMs > 0) {
try {
lock.isWaiting = true;
// The wait will take the timeout in unit of milli-seconds
lock.proceedLock.wait(nextTimeoutIntervalMs);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.isWaiting = false;
}
} else {
// break the loop if timeout happens
Expand All @@ -63,7 +66,9 @@ public void wakeUp() {
if (!lock.isToProceed) {
synchronized (lock.proceedLock) {
lock.isToProceed = true;
lock.proceedLock.notify();
if (lock.isWaiting) {
lock.proceedLock.notify();
}
}
}
}
Expand All @@ -73,9 +78,13 @@ private static final class RunnableLock {
private Object proceedLock;
private volatile boolean isToProceed;

// Are we doing a wait() on proceedLock.
private volatile boolean isWaiting;

RunnableLock() {
this.proceedLock = new Object();
isToProceed = false;
isWaiting = false;
}
}
}