Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
aloyszhang committed Sep 23, 2020
1 parent 6192b10 commit 23c02fe
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public abstract class TimerTask implements Runnable {

protected final long delayMs;
private TimerTaskEntry timerTaskEntry = null;
private volatile TimerTaskEntry timerTaskEntry = null;

protected TimerTask(long delayMs) {
this.delayMs = delayMs;
Expand All @@ -34,16 +34,19 @@ public synchronized void cancel() {
}
}

synchronized void setTimerTaskEntry(TimerTaskEntry entry) {
// if this timerTask is already held by an existing timer task entry,
// we will remove such an entry first.
if (null != timerTaskEntry && timerTaskEntry != entry) {
timerTaskEntry.remove();
void setTimerTaskEntry(TimerTaskEntry entry) {
synchronized (this) {
// if this timerTask is already held by an existing timer task entry,
// we will remove such an entry first.
if (null != timerTaskEntry && timerTaskEntry != entry) {
timerTaskEntry.remove();
}
timerTaskEntry = entry;
}
timerTaskEntry = entry;

}

synchronized TimerTaskEntry getTimerTaskEntry() {
TimerTaskEntry getTimerTaskEntry() {
return timerTaskEntry;
}

Expand Down

0 comments on commit 23c02fe

Please sign in to comment.