public class PeriodicTask extends Object implements Runnable, ScheduledFuture<Void>
Scheduler.
Can be used to control the task execution.
Sample Usage. The following code sketch demonstrates explicit construction of a periodic task.
class Agent {
private Scheduler scheduler;
private PeriodicTask heartbeat;
...
Agent() {
scheduler = new Scheduler("tester", 3);
heartbeat = new PeriodicTask(scheduler, this::broadcast, true, "heartbeat", Level.SEVERE, 5, TimeUnit.SECONDS);
...
}
public void start() {
...
heartbeat.start();
}
...
}
See Scheduler documentation for an example of creating an instance of
PeriodicTask by submitting a Runnuble to a Scheduler.| Constructor and Description |
|---|
PeriodicTask(Scheduler scheduler,
Runnable runnable,
boolean isFixedRate,
String taskName,
Level logLevel,
long period,
TimeUnit unit) |
PeriodicTask(Scheduler scheduler,
Runnable runnable,
boolean isFixedRate,
String taskName,
long period,
TimeUnit unit,
PeriodicTaskExceptionHandler exceptionHandler)
Creates a periodic task.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task.
|
int |
compareTo(Delayed o) |
Void |
get()
Blocks until the task is canceled.
|
Void |
get(long timeout,
TimeUnit unit)
Blocks until the task is canceled or the timeout expires.
|
long |
getDelay(TimeUnit unit)
Returns the remaining delay associated with this task, in the given time unit.
|
int |
getFailures()
Get the number of failures that occurred in this Periodic task.
|
Logger |
getLog()
Returns the logger used by this task.
|
Logger |
getLogger()
Deprecated.
Use
getLog() instead. |
Level |
getLogLevel()
Returns the log level set for reporting abnormal circumstances related to this task.
|
int |
getMaxFailures()
Get the maximum number of consecutive failures for this task.
|
long |
getPeriod(TimeUnit unit)
Returns execution period of this task.
|
String |
getTaskName()
Get the name of this PeriodicTask
|
Thread |
getThread()
Returns the thread currently executing this task.
|
boolean |
isCancelled()
Returns
true if this task has been canceled. |
boolean |
isDone()
Returns
true if this task has completed. |
static void |
main(String... args) |
void |
resetFailureCount()
Resets failure count.
|
void |
run() |
void |
setMaxFailures(int maxFailures)
Sets the maximum number of consecutive failures (uncaught exceptions) this task can
encounter before being suppressed.
|
void |
setPeriod(long period,
TimeUnit unit)
Modifies execution period of this task.
|
boolean |
start()
Starts executing this task if it is not already being executed.
|
boolean |
start(long initialDelay,
TimeUnit unit)
Starts executing this task if it is not already being executed.
|
void |
stop()
Suspends further executions of this periodic task.
|
void |
stop(long timeout,
TimeUnit unit)
Suspends further executions of this periodic task.
|
public PeriodicTask(Scheduler scheduler, Runnable runnable, boolean isFixedRate, String taskName, long period, TimeUnit unit, PeriodicTaskExceptionHandler exceptionHandler)
start(...)
methods is called.scheduler - Scheduler where this task will runrunnable - Runnable to executeisFixedRate - true if this task will be executed at fixed rate;
false if this task will be run with fixed delaytaskName - Name of this task;
if null, no name will be used.exceptionHandler - Handler that defines this task response to any abnormal circumstances;
if null, the default handler will be used.period - Period between successive executions, or the delay between executions,
depending on the value of isFixedRate argument.unit - Time unit of the period parameter.NullPointerException - if scheduler or runnable arguments are nullpublic int getFailures()
public int getMaxFailures()
public Thread getThread()
public long getDelay(TimeUnit unit)
public int compareTo(Delayed o)
compareTo in interface Comparable<Delayed>public boolean cancel(boolean mayInterruptIfRunning)
cancel is called,
this task should never run. If the task has already started, then the
mayInterruptIfRunning parameter determines whether the thread executing this
task should be interrupted in an attempt to stop the task.
After this method returns, subsequent calls to isDone() will
always return true. Subsequent calls to isCancelled()
will always return true if this method returned true.
cancel in interface Future<Void>mayInterruptIfRunning - true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed to completefalse if the task could not be canceled,
typically because it has already completed normally; true otherwisepublic boolean isCancelled()
true if this task has been canceled.isCancelled in interface Future<Void>public boolean isDone()
true if this task has completed.
Completion may be due to normal termination, an exception, or cancellation -
in all of these cases, this method will return true.public Void get() throws InterruptedException, ExecutionException
get in interface Future<Void>nullCancellationException - if the computation was canceledExecutionException - if the computation threw an exceptionInterruptedException - if the current thread was interrupted while waitingpublic Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get in interface Future<Void>timeout - the maximum time to waitunit - the time unit of the timeout argumentnullCancellationException - if the computation was canceledExecutionException - if the computation threw an exceptionInterruptedException - if the current thread was interrupted while waitingTimeoutException - if the wait timed outpublic void setMaxFailures(int maxFailures)
maxFailures - Maximum number of consecutive failures; if negative, the task is never suppressed.public void resetFailureCount()
public void setPeriod(long period,
TimeUnit unit)
stop(long, TimeUnit) before modifying
the period, then call start().period - the desired period.unit - the time unit of the period argumentpublic long getPeriod(TimeUnit unit)
unit - time unit for the returned valuepublic void stop()
start().
This method returns without waiting for the currently running execution of this task
to complete. Calling this method on a task that is already suspended has no effect.public void stop(long timeout,
TimeUnit unit)
throws InterruptedException,
TimeoutException
start().
This method blocks until the currently running execution of this task is complete, or the
timeout has expired. Calling this method on a task that is already suspended has no effect.timeout - the maximum time to wait; if 0, will wait indefinitelyunit - the time unit of the timeout argumentInterruptedException - if the current thread was interrupted while waitingTimeoutException - if the wait timed outpublic boolean start()
false if this task was already being executed; true otherwisepublic boolean start(long initialDelay,
TimeUnit unit)
initialDelay - the time to delay first executionunit - the time unit of the initialDelay parameterfalse if this task was already being executed; true otherwisepublic String getTaskName()
public Logger getLog()
null if no logger has been set
on the Scheduler where this task runs.@Deprecated public Logger getLogger()
getLog() instead.null if no logger has been set
on the Scheduler where this task runs.public Level getLogLevel()
public static void main(String... args)
Copyright © 2023 LSST. All rights reserved.