make ClockWatcher thread safe

This commit is contained in:
woodser 2024-03-28 13:34:01 -04:00
parent b0b5fde742
commit b9ed399036

View file

@ -54,6 +54,7 @@ public class ClockWatcher {
if (timer == null) {
lastSecondTick = System.currentTimeMillis();
timer = UserThread.runPeriodically(() -> {
synchronized (listeners) {
listeners.forEach(Listener::onSecondTick);
counter++;
if (counter >= 60) {
@ -73,6 +74,7 @@ public class ClockWatcher {
}
}
lastSecondTick = currentTimeMillis;
}
}, 1, TimeUnit.SECONDS);
}
}
@ -84,10 +86,14 @@ public class ClockWatcher {
}
public void addListener(Listener listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
public void removeListener(Listener listener) {
synchronized (listeners) {
listeners.remove(listener);
}
}
}