Class ComputationScheduler
- All Implemented Interfaces:
SchedulerMultiWorkerSupport
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class(package private) static final class(package private) static final classNested classes/interfaces inherited from class Scheduler
Scheduler.WorkerNested classes/interfaces inherited from interface SchedulerMultiWorkerSupport
SchedulerMultiWorkerSupport.WorkerCallback -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe name of the system property for setting the thread priority for this Scheduler.(package private) static final StringKey to setting the maximum number of computation scheduler threads.(package private) static final intThe maximum number of computation scheduler threads.(package private) static final ComputationScheduler.FixedSchedulerPoolThis will indicate no pool is active.(package private) final AtomicReference<ComputationScheduler.FixedSchedulerPool> (package private) static final ComputationScheduler.PoolWorker(package private) static final RxThreadFactoryprivate static final StringManages a fixed number of workers.(package private) final ThreadFactory -
Constructor Summary
ConstructorsConstructorDescriptionCreate a scheduler with pool size equal to the available processor count and using least-recent worker selection policy.ComputationScheduler(ThreadFactory threadFactory) Create a scheduler with pool size equal to the available processor count and using least-recent worker selection policy. -
Method Summary
Modifier and TypeMethodDescription(package private) static intcap(int cpuCount, int paramThreads) Retrieves or creates a newScheduler.Workerthat represents sequential execution of actions.voidcreateWorkers(int number, SchedulerMultiWorkerSupport.WorkerCallback callback) Creates the given number ofScheduler.Workerinstances that are possibly backed by distinct threads and calls the specifiedConsumerwith them.scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit) Schedules the execution of the given task with the given time delay.schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) Schedules a periodic execution of the given task with the given initial time delay and repeat period.voidshutdown()Instructs the Scheduler instance to stop threads, stop accepting tasks on any outstandingScheduler.Workerinstances and clean up any associated resources with this Scheduler.voidstart()Allows the Scheduler instance to start threads and accept tasks on them.Methods inherited from class Scheduler
clockDriftTolerance, now, scheduleDirect, when
-
Field Details
-
NONE
This will indicate no pool is active. -
THREAD_NAME_PREFIX
-
THREAD_FACTORY
-
KEY_MAX_THREADS
Key to setting the maximum number of computation scheduler threads. Zero or less is interpreted as use available. Capped by available.- See Also:
-
MAX_THREADS
static final int MAX_THREADSThe maximum number of computation scheduler threads. -
SHUTDOWN_WORKER
-
threadFactory
-
pool
-
KEY_COMPUTATION_PRIORITY
The name of the system property for setting the thread priority for this Scheduler.- See Also:
-
-
Constructor Details
-
ComputationScheduler
public ComputationScheduler()Create a scheduler with pool size equal to the available processor count and using least-recent worker selection policy. -
ComputationScheduler
Create a scheduler with pool size equal to the available processor count and using least-recent worker selection policy.- Parameters:
threadFactory- thread factory to use for creating worker threads. Note that this takes precedence over any system properties for configuring new thread creation. Cannot be null.
-
-
Method Details
-
cap
static int cap(int cpuCount, int paramThreads) -
createWorker
Description copied from class:SchedulerRetrieves or creates a newScheduler.Workerthat represents sequential execution of actions.When work is completed, the
Workerinstance should be released by callingDisposable.dispose()to avoid potential resource leaks in the underlying task-execution scheme.Work on a
Scheduler.Workeris guaranteed to be sequential and non-overlapping.- Specified by:
createWorkerin classScheduler- Returns:
- a Worker representing a serial queue of actions to be executed
-
createWorkers
Description copied from interface:SchedulerMultiWorkerSupportCreates the given number ofScheduler.Workerinstances that are possibly backed by distinct threads and calls the specifiedConsumerwith them.- Specified by:
createWorkersin interfaceSchedulerMultiWorkerSupport- Parameters:
number- the number of workers to create, positivecallback- the callback to send worker instances to
-
scheduleDirect
@NonNull public @NonNull Disposable scheduleDirect(@NonNull @NonNull Runnable run, long delay, TimeUnit unit) Description copied from class:SchedulerSchedules the execution of the given task with the given time delay.This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
- Overrides:
scheduleDirectin classScheduler- Parameters:
run- the task to scheduledelay- the delay amount, non-positive values indicate non-delayed schedulingunit- the unit of measure of the delay amount- Returns:
- the Disposable that let's one cancel this particular delayed task.
-
schedulePeriodicallyDirect
@NonNull public @NonNull Disposable schedulePeriodicallyDirect(@NonNull @NonNull Runnable run, long initialDelay, long period, TimeUnit unit) Description copied from class:SchedulerSchedules a periodic execution of the given task with the given initial time delay and repeat period.This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
The periodic execution is at a fixed rate, that is, the first execution will be after the
initialDelay, the second afterinitialDelay + period, the third afterinitialDelay + 2 * period, and so on.- Overrides:
schedulePeriodicallyDirectin classScheduler- Parameters:
run- the task to scheduleinitialDelay- the initial delay amount, non-positive values indicate non-delayed schedulingperiod- the period at which the task should be re-executedunit- the unit of measure of the delay amount- Returns:
- the Disposable that let's one cancel this particular delayed task.
-
start
public void start()Description copied from class:SchedulerAllows the Scheduler instance to start threads and accept tasks on them.Implementations should make sure the call is idempotent, thread-safe and should not throw any
RuntimeExceptionif it doesn't support this functionality. -
shutdown
public void shutdown()Description copied from class:SchedulerInstructs the Scheduler instance to stop threads, stop accepting tasks on any outstandingScheduler.Workerinstances and clean up any associated resources with this Scheduler.Implementations should make sure the call is idempotent, thread-safe and should not throw any
RuntimeExceptionif it doesn't support this functionality.
-