@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 and Type | Class and Description |
---|---|
(package private) static interface |
ScheduledChore.ChoreServicer |
Modifier and Type | Field and Description |
---|---|
private ScheduledChore.ChoreServicer |
choreServicer
Interface to the ChoreService that this ScheduledChore is scheduled with.
|
private static long |
DEFAULT_INITIAL_DELAY |
private static TimeUnit |
DEFAULT_TIME_UNIT
Default values for scheduling parameters should they be excluded during construction
|
private boolean |
initialChoreComplete |
private long |
initialDelay |
private static org.apache.commons.logging.Log |
LOG |
private String |
name |
private int |
period
Scheduling parameters.
|
private Stoppable |
stopper
A means by which a ScheduledChore can be stopped.
|
private long |
timeOfLastRun
Variables that encapsulate the meaningful state information
|
private long |
timeOfThisRun |
private TimeUnit |
timeUnit |
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
|
(package private) ScheduledChore.ChoreServicer |
getChoreServicer() |
long |
getInitialDelay() |
private double |
getMaximumAllowedTimeBetweenRuns() |
String |
getName() |
int |
getPeriod() |
Stoppable |
getStopper() |
(package private) long |
getTimeBetweenRuns() |
(package private) long |
getTimeOfLastRun() |
(package private) long |
getTimeOfThisRun() |
TimeUnit |
getTimeUnit() |
protected boolean |
initialChore()
Override to run a task before we start looping.
|
boolean |
isInitialChoreComplete() |
boolean |
isScheduled() |
private boolean |
isValidTime(long time) |
private boolean |
missedStartTime() |
private void |
onChoreMissedStartTime()
Notify the ChoreService that this chore has missed its start time.
|
void |
run() |
(package private) void |
setChoreServicer(ScheduledChore.ChoreServicer service) |
String |
toString() |
boolean |
triggerNow() |
private void |
updateTimeTrackingBeforeRun()
Update our time tracking members.
|
private static final org.apache.commons.logging.Log LOG
private final String name
private static final TimeUnit DEFAULT_TIME_UNIT
private static final long DEFAULT_INITIAL_DELAY
private final int period
private final TimeUnit timeUnit
private final long initialDelay
private ScheduledChore.ChoreServicer choreServicer
private long timeOfLastRun
private long timeOfThisRun
private boolean initialChoreComplete
private final Stoppable stopper
Stoppable.stop(String)
command can cause many chores to stop together.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 in millis 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 in millis 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 in Timeunit unit with which this Chore repeats execution when scheduled.initialDelay
- Delay in Timeunit unit 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()
private void updateTimeTrackingBeforeRun()
private void onChoreMissedStartTime()
long getTimeBetweenRuns()
private boolean missedStartTime()
private double getMaximumAllowedTimeBetweenRuns()
private boolean isValidTime(long time)
time
- in system millispublic boolean triggerNow()
void setChoreServicer(ScheduledChore.ChoreServicer service)
public void cancel()
public void cancel(boolean mayInterruptIfRunning)
public String getName()
public Stoppable getStopper()
public int getPeriod()
public long getInitialDelay()
public TimeUnit getTimeUnit()
public boolean isInitialChoreComplete()
ScheduledChore.ChoreServicer getChoreServicer()
long getTimeOfLastRun()
long getTimeOfThisRun()
public boolean isScheduled()
public void choreForTesting()
protected abstract void chore()
protected boolean initialChore()
protected void cleanup()
Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.