public final class Scheduler extends Object implements Executor
PeriodicTask
that provide control over the task execution: cancellation, change of period,
suspension and resumption. It is also possible to provide a logger for reporting
uncaught exceptions thrown by periodic tasks.
Sample Usage. The following code sketch creates a periodic task that beeps for 10 seconds, changing period after 3 seconds, then pausing for 2 seconds.
Scheduler scheduler = new Scheduler("tester", 1);
...
Runnable run = () -> java.awt.Toolkit.getDefaultToolkit().beep();
PeriodicTask task = scheduler.scheduleAtFixedRate(run, 0, 1, TimeUnit.SECONDS, "bipper", Level.SEVERE);
scheduler.schedule(() -> task.setPeriod(500, TimeUnit.MILLISECONDS), 3, TimeUnit.SECONDS);
scheduler.schedule(() -> task.stop(), 6, TimeUnit.SECONDS);
scheduler.schedule(() -> task.start(), 8, TimeUnit.SECONDS);
scheduler.schedule(() -> task.cancel(false), 10, TimeUnit.SECONDS);
...
scheduler.shutdown();
| Constructor and Description |
|---|
Scheduler(String name,
int nThreads)
Constructs a scheduler with a fixed number of executor threads.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the
timeout occurs, or the current thread is interrupted, whichever happens first.
|
void |
execute(Runnable command)
Executes a one-shot action.
|
int |
getActiveCount()
Get the (approximate) number of active threads for this scheduler.
|
Level |
getDefaultLogLevel()
Returns the default level at which uncaught exceptions thrown by scheduled tasks
are logged if no level is specified at task submission.
|
int |
getPoolSize()
Get the size of the pool, i.e the number of threads in the pool.
|
int |
getQueueSize()
Get the queue size.
|
boolean |
isShutdown()
Returns true if this scheduler has been shut down.
|
boolean |
isTerminated()
Returns true if all tasks have completed following shut down.
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
Date date)
Creates and executes a one-shot action that becomes enabled at the given time in the future.
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
|
ScheduledFuture<?> |
schedule(Runnable command,
Date date)
Creates and executes a one-shot action that becomes enabled at the given time in the future.
|
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
|
PeriodicTask |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit)
Creates and executes a fixedRate action that becomes enabled first after
the given initial delay, and subsequently with the given period.
|
PeriodicTask |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit,
String taskName,
Level logLevel)
Creates and executes a fixedRate action that becomes enabled first after
the given initial delay, and subsequently with the given period.
|
PeriodicTask |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit)
Creates and executes a fixedRate action that becomes enabled first after
the given initial delay, and subsequently with the given delay between
the termination of one execution and the commencement of the next.
|
PeriodicTask |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit,
String taskName,
Level logLevel)
Creates and executes a fixedRate action that becomes enabled first after
the given initial delay, and subsequently with the given delay between
the termination of one execution and the commencement of the next.
|
void |
setDefaultLogLevel(Level defaultLogLevel)
Modifies default level at which uncaught exceptions thrown by scheduled tasks
are logged if no level is specified at task submission.
|
void |
setLogger(Logger logger)
Sets logger to be used for reporting exceptions thrown by scheduled tasks.
|
void |
setMaxFailures(int maxFailures)
Sets the default maximum number of failures (uncaught exceptions) fixedRate tasks
can encounter before being suppressed.
|
void |
shutdown()
Initiates orderly shutdown of this scheduler.
|
void |
shutdownNow()
Initiates forced shutdown of this scheduler.
|
public Scheduler(String name, int nThreads)
name - name for threads created by this scheduler.nThreads - number of threads.public void shutdown()
public void shutdownNow()
public void setLogger(Logger logger)
public void setDefaultLogLevel(Level defaultLogLevel)
public void setMaxFailures(int maxFailures)
maxFailures - Maximum number of failures; if negative, the tasks are never suppressed.public Level getDefaultLogLevel()
public boolean isShutdown()
public boolean isTerminated()
public void execute(Runnable command)
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
command - the task to executedelay - the time from now to delay executionunit - the time unit of the delay parameterget()
method will return null upon completionRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if command is nullpublic ScheduledFuture<?> schedule(Runnable command, Date date)
command - the task to execute.date - the time when the execution should startget()
method will return null upon completionRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if command is nullpublic <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
V - the type of the resultcallable - the function to executedelay - the time from now to delay executionunit - the time unit of the delay parameterRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if callable is nullpublic <V> ScheduledFuture<V> schedule(Callable<V> callable, Date date)
V - the type of the resultcallable - the function to executedate - the time when the execution should startRejectedExecutionException - if the task cannot be
scheduled for executionNullPointerException - if callable is nullpublic PeriodicTask scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit, String taskName, Level logLevel)
command - the task to executeinitialDelay - the time to delay first executionperiod - the period between successive executionsunit - the time unit of the initialDelay and period parameterstaskName - task name, used to identify failed tasks in the loglogLevel - level at which exceptions thrown by scheduled tasks should be loggedget() method will throw an exception upon cancellationRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if command is nullIllegalArgumentException - if period less than or equal to zeropublic PeriodicTask scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
command - the task to executeinitialDelay - the time to delay first executionperiod - the period between successive executionsunit - the time unit of the initialDelay and period parametersget() method will throw an exception upon cancellationRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if command is nullIllegalArgumentException - if period less than or equal to zeropublic PeriodicTask scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit, String taskName, Level logLevel)
command - the task to executeinitialDelay - the time to delay first executiondelay - the delay between the termination of one
execution and the commencement of the nextunit - the time unit of the initialDelay and delay parameterstaskName - task name, used to identify failed tasks in the loglogLevel - level at which exceptions thrown by scheduled tasks should be loggedget() method will throw an exception upon cancellationRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if command is nullIllegalArgumentException - if delay less than or equal to zeropublic PeriodicTask scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
command - the task to executeinitialDelay - the time to delay first executiondelay - the delay between the termination of one
execution and the commencement of the nextunit - the time unit of the initialDelay and delay parametersget() method will throw an exception upon cancellationRejectedExecutionException - if the task cannot be scheduled for executionNullPointerException - if command is nullIllegalArgumentException - if delay less than or equal to zeropublic boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
timeout - the maximum time to wait.unit - the time unit of the timeout argument.InterruptedException - if interrupted while waitingpublic int getActiveCount()
public int getQueueSize()
public int getPoolSize()
Copyright © 2019 LSST. All rights reserved.