public class Watchdog
extends java.lang.Object
The task will execute once the specified delay has passed unless the watchdog is "fed" (reset) or its timeout is "delayed". Additionally, the watchdog task can be cancelled entirely.
This implementation uses a ReentrantLock
to protect the critical sections where task
cancellation and rescheduling happen, ensuring thread-safe execution even when multiple threads
concurrently call methods like feed()
and feedTreat(long, TimeUnit)
.
A ScheduledExecutorService
with a fixed thread pool size of 2 is used to schedule the tasks.
This ensures concurrent execution of watchdog tasks, allowing multiple watchdogs to operate independently
and execute tasks in parallel if needed.
Modifier and Type | Method and Description |
---|---|
boolean |
callOff()
Cancels the current watchdog task if it exists.
|
boolean |
feed()
Reschedules the watchdog with the initially configured delay.
|
boolean |
feedTreat(int oneTimeDelaySeconds)
Prolongs the watchdog with a one-time delay, specified in seconds.
|
boolean |
feedTreat(long oneTimeDelay,
java.util.concurrent.TimeUnit timeUnit)
Prolongs the watchdog with a one-time delay.
|
static void |
main(java.lang.String[] args)
Main method to demonstrate the use of the Watchdog.
|
static Watchdog |
of(java.lang.Runnable task,
int delaySeconds)
Factory method to create a new instance of the
Watchdog . |
static Watchdog |
of(java.lang.Runnable task,
long delay,
java.util.concurrent.TimeUnit timeUnit)
Factory method to create a new instance of the
Watchdog . |
public static Watchdog of(java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit timeUnit)
Watchdog
.task
- The task to execute when the watchdog triggers.delay
- The initial delay before the watchdog triggers.timeUnit
- The time unit of the delay.Watchdog
instance.public static Watchdog of(java.lang.Runnable task, int delaySeconds)
Watchdog
.task
- The task to execute when the watchdog triggers.delaySeconds
- The initial delay before the watchdog triggers, in seconds.Watchdog
instance.public boolean callOff()
This method cancels the currently scheduled task, if it is still pending execution. It ensures that no task will be executed once the watchdog is called off.
public boolean feed()
This method resets the watchdog, essentially "feeding" it, by canceling the existing task and scheduling a new task with the original delay. The task will trigger after the initial delay unless rescheduled again.
public boolean feedTreat(int oneTimeDelaySeconds)
This method provides the watchdog with a special "treat" by delaying its next trigger with a one-time delay, specified in seconds.
The watchdog will revert to its original triggering schedule when "fed" via feed()
normally again.
This is useful for situations where a longer operation needs to be performed without causing the watchdog to bark prematurely.
oneTimeDelaySeconds
- The one-time delay, in seconds, to apply to the watchdog.true
if the watchdog's triggering was successfully delayed, false
otherwise.public boolean feedTreat(long oneTimeDelay, java.util.concurrent.TimeUnit timeUnit)
This method provides the watchdog with a special "treat" by delaying its next trigger with a one-time delay.
The watchdog will revert to its original triggering schedule when "fed" via feed()
normally again.
This is useful for situations where a longer operation needs to be performed without causing the watchdog to bark prematurely.
oneTimeDelay
- The one-time delay to apply to the watchdog.timeUnit
- The time unit of the delay.true
if the watchdog's triggering was successfully delayed, false
otherwise.public static void main(java.lang.String[] args) throws java.lang.InterruptedException
This example shows how to create a Watchdog, "feed" it (reset its timer), "delay" its barking (prolong its timer) and finally let it bark (trigger the task).
args
- command-line argumentsjava.lang.InterruptedException
- if interrupted while waiting for tasksCopyright © 2000-2024 OAshi S.à r.l. All Rights Reserved.