move synchronization one method call upwards.
This avoids a bug, where removing a timer interferes with exporting data,
i.e., one value would be lost.
@@ -97,11 +97,9 @@ public class TimerPool {
private long eval(Function<Timer, Long> f, BiFunction<Long, Long, Long> acc) {
long currentInterval = 0;
- synchronized (activeTimers) {
for (Timer timer : activeTimers.values()) {
currentInterval = acc.apply(currentInterval, f.apply(timer));
}
- }
return currentInterval;
}
@@ -111,8 +109,10 @@ public class TimerPool {
*/
public TimeSpan getSumTime() {
cleanupReferences();
+ synchronized (activeTimers) {
return export(sumTime + eval(t -> convert(t.getSumTime()), Math::addExact));
}
+ }
/**
* Return the maximal time of all intervals. If timers are running, the current intervals are also
@@ -120,8 +120,10 @@ public class TimerPool {
*/
public TimeSpan getMaxTime() {
cleanupReferences();
+ synchronized (activeTimers) {
return export(Math.max(maxTime, eval(t -> convert(t.getMaxTime()), Math::max)));
}
+ }
/**
* Return the number of intervals. If timers are running, the current intervals are also counted.
@@ -129,8 +131,10 @@ public class TimerPool {
*/
public int getNumberOfIntervals() {
cleanupReferences();
+ synchronized (activeTimers) {
return (int) (numberOfIntervals + eval(t -> (long) t.getNumberOfIntervals(), Math::addExact));
}
+ }
private TimeSpan export(long time) {
return TimeSpan.of(time, UNIT);
