@InterfaceAudience.Private public class ChoreService extends Object
ScheduledChore
to run
periodically while sharing threads. The ChoreService is backed by a
ScheduledThreadPoolExecutor
whose core pool size changes dynamically depending on the
number of ScheduledChore
scheduled. All of the threads in the core thread pool of the
underlying ScheduledThreadPoolExecutor
are set to be daemon threads.
The ChoreService provides the ability to schedule, cancel, and trigger instances of
ScheduledChore
. The ChoreService also provides the ability to check on the status of
scheduled chores. The number of threads used by the ChoreService changes based on the scheduling
load and whether or not the scheduled chores are executing on time. As more chores are scheduled,
there may be a need to increase the number of threads if it is noticed that chores are no longer
meeting their scheduled start times. On the other hand, as chores are cancelled, an attempt is
made to reduce the number of running threads to see if chores can still meet their start times
with a smaller thread pool.
When finished with a ChoreService it is good practice to call shutdown()
.
Calling this method ensures that all scheduled chores are cancelled and cleaned up properly.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
ChoreService.ChoreServiceThreadFactory
Custom ThreadFactory used with the ScheduledThreadPoolExecutor so that all the threads are
daemon threads, and thus, don't prevent the JVM from shutting down
|
Modifier and Type | Field and Description |
---|---|
private HashMap<ScheduledChore,Boolean> |
choresMissingStartTime
Maps chores to Booleans which indicate whether or not a chore has caused an increase in the
core pool size of the ScheduledThreadPoolExecutor.
|
private String |
coreThreadPoolPrefix
The coreThreadPoolPrefix is the prefix that will be applied to all threads within the
ScheduledThreadPoolExecutor.
|
private static org.slf4j.Logger |
LOG |
static int |
MIN_CORE_POOL_SIZE
The minimum number of threads in the core pool of the underlying ScheduledThreadPoolExecutor
|
private HashMap<ScheduledChore,ScheduledFuture<?>> |
scheduledChores
Maps chores to their futures.
|
private ScheduledThreadPoolExecutor |
scheduler
This thread pool is used to schedule all of the Chores
|
Constructor and Description |
---|
ChoreService(String coreThreadPoolPrefix) |
ChoreService(String coreThreadPoolPrefix,
boolean jitter) |
ChoreService(String coreThreadPoolPrefix,
int corePoolSize,
boolean jitter) |
Modifier and Type | Method and Description |
---|---|
private void |
cancelAllChores(boolean mayInterruptIfRunning) |
(package private) void |
cancelChore(ScheduledChore chore)
Cancel any ongoing schedules that this chore has with the implementer of this interface.
|
(package private) void |
cancelChore(ScheduledChore chore,
boolean mayInterruptIfRunning)
Cancel any ongoing schedules that this chore has with the implementer of this interface.
|
(package private) int |
getCorePoolSize()
Returns number of threads in the core pool of the underlying ScheduledThreadPoolExecutor
|
(package private) int |
getNumberOfChoresMissingStartTime()
Return number of chores that this service currently has scheduled that are missing their
scheduled start time
|
(package private) int |
getNumberOfScheduledChores()
Returns number of chores that this service currently has scheduled
|
boolean |
isChoreScheduled(ScheduledChore chore)
Returns true when the chore is scheduled with the implementer of this interface
|
boolean |
isShutdown()
Returns true when the service is shutdown and thus cannot be used anymore
|
boolean |
isTerminated()
Returns true when the service is shutdown and all threads have terminated
|
(package private) void |
onChoreMissedStartTime(ScheduledChore chore)
A callback that tells the implementer of this interface that one of the scheduled chores is
missing its start time.
|
private void |
printChoreDetails(String header,
ScheduledChore chore)
Prints a summary of important details about the chore.
|
private void |
printChoreServiceDetails(String header)
Prints a summary of important details about the service.
|
private void |
requestCorePoolDecrease()
Represents a request to decrease the number of core pool threads.
|
private boolean |
requestCorePoolIncrease()
Represents a request to increase the number of core pool threads.
|
private void |
rescheduleChore(ScheduledChore chore) |
boolean |
scheduleChore(ScheduledChore chore)
Schedule a chore.
|
void |
shutdown()
Shut down the service.
|
(package private) void |
triggerNow(ScheduledChore chore)
This method tries to execute the chore immediately.
|
private static final org.slf4j.Logger LOG
@InterfaceAudience.Private public static final int MIN_CORE_POOL_SIZE
private final ScheduledThreadPoolExecutor scheduler
private final HashMap<ScheduledChore,ScheduledFuture<?>> scheduledChores
private final HashMap<ScheduledChore,Boolean> choresMissingStartTime
private final String coreThreadPoolPrefix
@InterfaceAudience.Private public ChoreService(String coreThreadPoolPrefix)
coreThreadPoolPrefix
- Prefix that will be applied to the Thread name of all threads
spawned by this servicepublic ChoreService(String coreThreadPoolPrefix, boolean jitter)
coreThreadPoolPrefix
- Prefix that will be applied to the Thread name of all threads
spawned by this servicejitter
- Should chore service add some jitter for all of the scheduled
chores. When set to true this will add -10% to 10% jitter.public ChoreService(String coreThreadPoolPrefix, int corePoolSize, boolean jitter)
coreThreadPoolPrefix
- Prefix that will be applied to the Thread name of all threads
spawned by this servicecorePoolSize
- The initial size to set the core pool of the
ScheduledThreadPoolExecutor to during initialization. The default
size is 1, but specifying a larger size may be beneficial if you
know that 1 thread will not be enough.jitter
- Should chore service add some jitter for all of the scheduled
chores. When set to true this will add -10% to 10% jitter.public boolean scheduleChore(ScheduledChore chore)
chore
- Chore to be scheduled. If the chore is already scheduled with another ChoreService
instance, that schedule will be cancelled (i.e. a Chore can only ever be scheduled
with a single ChoreService instance).private void rescheduleChore(ScheduledChore chore)
chore
- The Chore to be rescheduled. If the chore is not scheduled with this ChoreService
yet then this call is equivalent to a call to scheduleChore.void cancelChore(ScheduledChore chore)
ScheduledChore.cancel()
to cancel a ScheduledChore
, in
ScheduledChore.cancel()
method we will call this method to remove the
ScheduledChore
from this ChoreService
.void cancelChore(ScheduledChore chore, boolean mayInterruptIfRunning)
ScheduledChore.cancel(boolean)
to cancel a ScheduledChore
, in
ScheduledChore.cancel(boolean)
method we will call this method to remove the
ScheduledChore
from this ChoreService
.@InterfaceAudience.Private public boolean isChoreScheduled(ScheduledChore chore)
void triggerNow(ScheduledChore chore)
int getNumberOfScheduledChores()
int getNumberOfChoresMissingStartTime()
int getCorePoolSize()
private boolean requestCorePoolIncrease()
private void requestCorePoolDecrease()
void onChoreMissedStartTime(ScheduledChore chore)
chore
- The chore that missed its start timepublic void shutdown()
public boolean isShutdown()
public boolean isTerminated()
private void cancelAllChores(boolean mayInterruptIfRunning)
private void printChoreDetails(String header, ScheduledChore chore)
private void printChoreServiceDetails(String header)
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.