public final class TaskScheduler
extends java.lang.Object
ScheduledExecutorService
, scheduled at a fixed rate,
using a daemon thread by default.
A TaskScheduler can be stopped and restarted.
TaskScheduler scheduler = new TaskScheduler(new Runnable() { public void run() { System.out.println("Running wild..."); } }, 2, TimeUnit.SECONDS); scheduler.start(); ... scheduler.setInterval(1);//task restarted using the new interval ... scheduler.stop();
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
INTERVAL_PROPERTY |
Constructor and Description |
---|
TaskScheduler(java.lang.Runnable task,
int interval,
int initialDelay,
java.util.concurrent.TimeUnit timeUnit)
Instantiates a new TaskScheduler instance with a daemon thread.
|
TaskScheduler(java.lang.Runnable task,
int interval,
int initialDelay,
java.util.concurrent.TimeUnit timeUnit,
java.util.concurrent.ThreadFactory threadFactory)
Instantiates a new TaskScheduler instance.
|
TaskScheduler(java.lang.Runnable task,
int interval,
java.util.concurrent.TimeUnit timeUnit)
Instantiates a new TaskScheduler instance, with no initial delay and a daemon thread
|
Modifier and Type | Method and Description |
---|---|
int |
getInterval() |
EventObserver<java.lang.Integer> |
getIntervalObserver() |
boolean |
isRunning() |
void |
setInterval(int interval)
Sets the new task interval and re-schedules the task, note that if the scheduler was stopped it will be restarted.
|
TaskScheduler |
start()
Starts this TaskScheduler, if it is running it is restarted, using the initial delay specified during construction.
|
void |
stop()
Stops this TaskScheduler, if it is not running calling this method has no effect.
|
public static final java.lang.String INTERVAL_PROPERTY
public TaskScheduler(java.lang.Runnable task, int interval, java.util.concurrent.TimeUnit timeUnit)
task
- the task to runinterval
- the intervaltimeUnit
- the time unit to usepublic TaskScheduler(java.lang.Runnable task, int interval, int initialDelay, java.util.concurrent.TimeUnit timeUnit)
task
- the task to runinterval
- the intervalinitialDelay
- the initial start delay, used on restarts as welltimeUnit
- the time unit to usepublic TaskScheduler(java.lang.Runnable task, int interval, int initialDelay, java.util.concurrent.TimeUnit timeUnit, java.util.concurrent.ThreadFactory threadFactory)
task
- the task to runinterval
- the intervalinitialDelay
- the initial start delay, used on restarts as welltimeUnit
- the time unit to usethreadFactory
- the thread factory to usepublic int getInterval()
public void setInterval(int interval)
interval
- the intervaljava.lang.IllegalArgumentException
- in case interval
isn't a positive integerpublic EventObserver<java.lang.Integer> getIntervalObserver()
public TaskScheduler start()
public void stop()
public boolean isRunning()