Skip to content

Commit 5972b1e

Browse files
authored
Make TimerImpl thread-safe (#5051)
Signed-off-by: Ravi Nadahar <[email protected]>
1 parent aa73ac8 commit 5972b1e

File tree

1 file changed

+9
-6
lines changed
  • bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/action

1 file changed

+9
-6
lines changed

bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/action/TimerImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
@NonNullByDefault
3030
public class TimerImpl implements Timer {
3131

32+
// All access must be guarded by "this"
3233
private final Scheduler scheduler;
3334
private final SchedulerRunnable runnable;
3435
private final @Nullable String identifier;
36+
37+
// All access must be guarded by "this"
3538
private ScheduledCompletableFuture<?> future;
3639

3740
public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable) {
@@ -48,7 +51,7 @@ public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable
4851
}
4952

5053
@Override
51-
public boolean cancel() {
54+
public synchronized boolean cancel() {
5255
return future.cancel(true);
5356
}
5457

@@ -60,27 +63,27 @@ public synchronized boolean reschedule(ZonedDateTime newTime) {
6063
}
6164

6265
@Override
63-
public @Nullable ZonedDateTime getExecutionTime() {
66+
public synchronized @Nullable ZonedDateTime getExecutionTime() {
6467
return future.isCancelled() ? null : future.getScheduledTime();
6568
}
6669

6770
@Override
68-
public boolean isActive() {
71+
public synchronized boolean isActive() {
6972
return !future.isDone();
7073
}
7174

7275
@Override
73-
public boolean isCancelled() {
76+
public synchronized boolean isCancelled() {
7477
return future.isCancelled();
7578
}
7679

7780
@Override
78-
public boolean isRunning() {
81+
public synchronized boolean isRunning() {
7982
return isActive() && ZonedDateTime.now().isAfter(future.getScheduledTime());
8083
}
8184

8285
@Override
83-
public boolean hasTerminated() {
86+
public synchronized boolean hasTerminated() {
8487
return future.isDone();
8588
}
8689
}

0 commit comments

Comments
 (0)