@InterfaceAudience.Private public abstract class ScheduledChore extends Object implements Runnable
ChoreService
via ChoreService.scheduleChore(ScheduledChore)
. The
chore is run in a ScheduledThreadPoolExecutor
and competes with other ScheduledChores for
access to the threads in the core thread pool. If an unhandled exception occurs, the chore
cancellation is logged. Implementers should consider whether or not the Chore will be able to
execute within the defined period. It is bad practice to define a ScheduledChore whose execution
time exceeds its period since it will try to hog one of the threads in the ChoreService
's
thread pool.
Don't subclass ScheduledChore if the task relies on being woken up for something to do, such as an entry being added to a queue, etc.
Modifier | Constructor and Description |
---|---|
protected |
ScheduledChore()
This constructor is for test only.
|
|
ScheduledChore(String name,
Stoppable stopper,
int period) |
|
ScheduledChore(String name,
Stoppable stopper,
int period,
long initialDelay) |
|
ScheduledChore(String name,
Stoppable stopper,
int period,
long initialDelay,
TimeUnit unit) |
Modifier and Type | Method and Description |
---|---|
void |
cancel() |
void |
cancel(boolean mayInterruptIfRunning) |
protected abstract void |
chore()
The task to execute on each scheduled execution of the Chore
|
void |
choreForTesting() |
protected void |
cleanup()
Override to run cleanup tasks when the Chore encounters an error and must stop running
|
long |
getInitialDelay() |
String |
getName() |
int |
getPeriod() |
Stoppable |
getStopper() |
TimeUnit |
getTimeUnit() |
protected boolean |
initialChore()
Override to run a task before we start looping.
|
boolean |
isInitialChoreComplete() |
boolean |
isScheduled() |
void |
run() |
String |
toString() |
boolean |
triggerNow() |
protected ScheduledChore()
public ScheduledChore(String name, Stoppable stopper, int period)
name
- Name assigned to Chore. Useful for identification amongst chores of the same typestopper
- When Stoppable.isStopped()
is true, this chore will cancel and cleanupperiod
- Period with which this Chore repeats execution when scheduled.public ScheduledChore(String name, Stoppable stopper, int period, long initialDelay)
name
- Name assigned to Chore. Useful for identification amongst chores of the same typestopper
- When Stoppable.isStopped()
is true, this chore will cancel and cleanupperiod
- Period with which this Chore repeats execution when scheduled.initialDelay
- Delay before this Chore begins to execute once it has been scheduled. A
value of 0 means the chore will begin to execute immediately. Negative delays are
invalid and will be corrected to a value of 0.public ScheduledChore(String name, Stoppable stopper, int period, long initialDelay, TimeUnit unit)
name
- Name assigned to Chore. Useful for identification amongst chores of the same typestopper
- When Stoppable.isStopped()
is true, this chore will cancel and cleanupperiod
- Period with which this Chore repeats execution when scheduled.initialDelay
- Delay before this Chore begins to execute once it has been scheduled. A
value of 0 means the chore will begin to execute immediately. Negative delays are
invalid and will be corrected to a value of 0.unit
- The unit that is used to measure period and initialDelaypublic void run()
run
in interface Runnable
Thread.run()
public boolean triggerNow()
public void cancel()
public void cancel(boolean mayInterruptIfRunning)
public String getName()
public Stoppable getStopper()
public int getPeriod()
public long getInitialDelay()
public final TimeUnit getTimeUnit()
public boolean isInitialChoreComplete()
public boolean isScheduled()
public void choreForTesting()
protected abstract void chore()
protected boolean initialChore()
protected void cleanup()