Skip to content

Commit

Permalink
Fix TimeUtilities thread number counting synchronization
Browse files Browse the repository at this point in the history
Use AtomicInteger to update the thread number atomically.

Signed-off-by: Jason Feng <[email protected]>
  • Loading branch information
JasonFengJ9 committed Nov 19, 2024
1 parent 44766de commit 07d589c
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Utility class to check time.
Expand Down Expand Up @@ -99,9 +100,9 @@ public static long getCurrentTimeInNanoseconds() {
}

private volatile boolean tasksPassed = true;
private volatile int taskRunning = 0;
private volatile int taskStarted = 0;
private volatile int taskExecuted = 0;
private AtomicInteger taskRunning = new AtomicInteger(0);
private AtomicInteger taskStarted = new AtomicInteger(0);
private AtomicInteger taskExecuted = new AtomicInteger(0);
private final ArrayList<Timer> timers = new ArrayList<Timer>();

public void timerSchedule(String testName, long startMillisTime, long startNanoTime, long minElapsedMillisTime,
Expand All @@ -113,19 +114,21 @@ public void timerSchedule(String testName, long startMillisTime, long startNanoT
}

public boolean getResultAndCancelTimers() {
showThreadCurrentTime("getResultAndCancelTimers before Thread.yield() taskRunning = " + taskRunning);
while (taskRunning > 0) {
showThreadCurrentTime("getResultAndCancelTimers before Thread.yield() taskRunning = " + taskRunning.get()
+ ", taskStarted: " + taskStarted.get() + ", taskExecuted: " + taskExecuted.get());
while (taskRunning.get() > 0) {
Thread.yield();
}

showThreadCurrentTime("getResultAndCancelTimers before timer.cancel()");
showThreadCurrentTime("getResultAndCancelTimers before timer.cancel() taskRunning = " + taskRunning.get()
+ ", taskStarted: " + taskStarted.get() + ", taskExecuted: " + taskExecuted.get());
for (Timer timer : timers) {
timer.cancel();
}

showThreadCurrentTime("TimeTestTask tasksPassed: " + tasksPassed + ", taskStarted: " + taskStarted
+ ", taskExecuted: " + taskExecuted);
return tasksPassed && (taskStarted == taskExecuted);
showThreadCurrentTime("TimeTestTask tasksPassed: " + tasksPassed + ", taskStarted: " + taskStarted.get()
+ ", taskExecuted: " + taskExecuted.get());
return tasksPassed && (taskStarted.get() == taskExecuted.get());
}

class TimeTestTask extends TimerTask {
Expand All @@ -148,16 +151,18 @@ class TimeTestTask extends TimerTask {
this.minElapsedNanoTimeInMillis = minElapsedNanoTimeInMillis;
this.maxElapsedNanoTimeInMillis = maxElapsedNanoTimeInMillis;

taskStarted++;
taskRunning++;
taskRunning.incrementAndGet();
taskStarted.incrementAndGet();
}

public void run() {
taskExecuted++;
taskExecuted.incrementAndGet();
if (!checkElapseTime(testName, startMillisTime, startNanoTime, minElapsedMillisTime, minElapsedNanoTimeInMillis)) {
tasksPassed = false;
}
taskRunning--;
taskRunning.decrementAndGet();
showThreadCurrentTime("TimeTestTask.run() taskRunning = " + taskRunning.get() + ", taskStarted: " + taskStarted.get()
+ ", taskExecuted: " + taskExecuted.get());
}
}
}

0 comments on commit 07d589c

Please sign in to comment.