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)
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.
|
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
|
boolean |
isCancelled()
Returns
true if this task has been canceled. |
boolean |
isDone()
Returns
true if this task has completed. |
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, Level logLevel, long period, TimeUnit unit)
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 usedlogLevel - level at which exceptions thrown by this task should be logged;
if null, the default level for the specified Scheduler will be usedperiod - the period between successive executions, or the delay between executions,
depending on the value of isFixedRate argumentunit - the time unit of the period parameterNullPointerException - if scheduler or runnable arguments are nullpublic int getFailures()
public int getMaxFailures()
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 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()
Copyright © 2020 LSST. All rights reserved.