-
-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Timer.isCancelled() method #2570
Conversation
4a7b306
to
d4cb926
Compare
Signed-off-by: Jimmy Tanagra <[email protected]>
d4cb926
to
15d49e4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks.
@@ -48,26 +46,29 @@ public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable | |||
|
|||
@Override | |||
public boolean cancel() { | |||
cancelled = true; | |||
return future.cancel(true); | |||
} | |||
|
|||
@Override | |||
public boolean reschedule(ZonedDateTime newTime) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to your contribution but I am wondering if we should synchronize this method to have a thread safe implementation? Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears to be thread safe already https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html#cancel(boolean)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I am talking about is a small race condition on the future
property of this implementation when calling the reschedule(...)
method because we first cancel the existing future and afterwards assign a new CompletableFuture
to it.
Signed-off-by: Jimmy Tanagra <[email protected]> GitOrigin-RevId: a4b737c
I found the need for Timer.isCancelled() inside a long running timer that self-reschedules itself.
Example:
When Switch1 received a command ON while
downloadFile()
is executing, the timer got cancelled, but the currently running task is still continuing, it will reschedule the timer and therefore the timer continues to run despite having been "cancelled".In this case, the timer code can check with
isCancelled()
before callingreschedule
as such: