@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 | Field and Description |
---|---|
private ChoreService |
choreService
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.slf4j.Logger |
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()
Construct a ScheduledChore
|
|
ScheduledChore(String name,
Stoppable stopper,
int period)
Construct a ScheduledChore
|
|
ScheduledChore(String name,
Stoppable stopper,
int period,
long initialDelay)
Construct a ScheduledChore
|
|
ScheduledChore(String name,
Stoppable stopper,
int period,
long initialDelay,
TimeUnit unit)
Construct a ScheduledChore
|
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) ChoreService |
getChoreService() |
long |
getInitialDelay()
Returns initial delay before executing chore in getTimeUnit() units
|
private double |
getMaximumAllowedTimeBetweenRuns()
Returns max allowed time in millis between runs.
|
String |
getName() |
int |
getPeriod()
Returns period to execute chore in getTimeUnit() units
|
Stoppable |
getStopper() |
(package private) long |
getTimeBetweenRuns()
Return how long in millis has it been since this chore last run.
|
(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()
Returns true when this Chore is scheduled with a ChoreService
|
private boolean |
isValidTime(long time)
Return true if time is earlier or equal to current time
|
private boolean |
missedStartTime()
Returns true when the time between runs exceeds the acceptable threshold
|
private void |
onChoreMissedStartTime()
Notify the ChoreService that this chore has missed its start time.
|
void |
run() |
(package private) void |
setChoreService(ChoreService service) |
void |
shutdown()
Call
shutdown(boolean) with true . |
void |
shutdown(boolean mayInterruptIfRunning)
Completely shutdown the ScheduleChore, which means we will call cleanup and you should not
schedule it again.
|
String |
toString()
A summation of this chore in human readable format.
|
boolean |
triggerNow()
Returns false when the Chore is not currently scheduled with a ChoreService
|
private void |
updateTimeTrackingBeforeRun()
Update our time tracking members.
|
private static final org.slf4j.Logger LOG
private static final TimeUnit DEFAULT_TIME_UNIT
private static final long DEFAULT_INITIAL_DELAY
private final int period
private final long initialDelay
private ChoreService choreService
private long timeOfLastRun
private long timeOfThisRun
private boolean initialChoreComplete
private final Stoppable stopper
Stoppable.stop(String)
command can cause many chores to stop together.@InterfaceAudience.Private protected ScheduledChore()
This constructor is for test only. It allows us to create an object and to call chore() on it.
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 initialDelayprivate void updateTimeTrackingBeforeRun()
private void onChoreMissedStartTime()
long getTimeBetweenRuns()
private boolean missedStartTime()
private double getMaximumAllowedTimeBetweenRuns()
private boolean isValidTime(long time)
public boolean triggerNow()
void setChoreService(ChoreService service)
public void cancel()
public void cancel(boolean mayInterruptIfRunning)
public Stoppable getStopper()
public int getPeriod()
public long getInitialDelay()
public TimeUnit getTimeUnit()
public boolean isInitialChoreComplete()
ChoreService getChoreService()
long getTimeOfLastRun()
long getTimeOfThisRun()
public boolean isScheduled()
@InterfaceAudience.Private public void choreForTesting()
protected abstract void chore()
protected boolean initialChore()
protected void cleanup()
public void shutdown()
shutdown(boolean)
with true
.shutdown(boolean)
public void shutdown(boolean mayInterruptIfRunning)
@InterfaceAudience.Private public String toString()
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.