Class ComputationScheduler
- java.lang.Object
-
- io.reactivex.rxjava3.core.Scheduler
-
- io.reactivex.rxjava3.internal.schedulers.ComputationScheduler
-
- All Implemented Interfaces:
SchedulerMultiWorkerSupport
public final class ComputationScheduler extends Scheduler implements SchedulerMultiWorkerSupport
Holds a fixed pool of worker threads and assigns them to requested Scheduler.Workers in a round-robin fashion.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
ComputationScheduler.EventLoopWorker
(package private) static class
ComputationScheduler.FixedSchedulerPool
(package private) static class
ComputationScheduler.PoolWorker
-
Nested classes/interfaces inherited from class io.reactivex.rxjava3.core.Scheduler
Scheduler.Worker
-
Nested classes/interfaces inherited from interface io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport
SchedulerMultiWorkerSupport.WorkerCallback
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
KEY_COMPUTATION_PRIORITY
The name of the system property for setting the thread priority for this Scheduler.(package private) static java.lang.String
KEY_MAX_THREADS
Key to setting the maximum number of computation scheduler threads.(package private) static int
MAX_THREADS
The maximum number of computation scheduler threads.(package private) static ComputationScheduler.FixedSchedulerPool
NONE
This will indicate no pool is active.(package private) java.util.concurrent.atomic.AtomicReference<ComputationScheduler.FixedSchedulerPool>
pool
(package private) static ComputationScheduler.PoolWorker
SHUTDOWN_WORKER
(package private) static RxThreadFactory
THREAD_FACTORY
private static java.lang.String
THREAD_NAME_PREFIX
Manages a fixed number of workers.(package private) java.util.concurrent.ThreadFactory
threadFactory
-
Constructor Summary
Constructors Constructor Description ComputationScheduler()
Create a scheduler with pool size equal to the available processor count and using least-recent worker selection policy.ComputationScheduler(java.util.concurrent.ThreadFactory threadFactory)
Create a scheduler with pool size equal to the available processor count and using least-recent worker selection policy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static int
cap(int cpuCount, int paramThreads)
@NonNull Scheduler.Worker
createWorker()
Retrieves or creates a newScheduler.Worker
that represents sequential execution of actions.void
createWorkers(int number, SchedulerMultiWorkerSupport.WorkerCallback callback)
Creates the given number ofScheduler.Worker
instances that are possibly backed by distinct threads and calls the specifiedConsumer
with them.@NonNull Disposable
scheduleDirect(@NonNull java.lang.Runnable run, long delay, java.util.concurrent.TimeUnit unit)
Schedules the execution of the given task with the given time delay.@NonNull Disposable
schedulePeriodicallyDirect(@NonNull java.lang.Runnable run, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
Schedules a periodic execution of the given task with the given initial time delay and repeat period.void
shutdown()
Instructs the Scheduler instance to stop threads, stop accepting tasks on any outstandingScheduler.Worker
instances and clean up any associated resources with this Scheduler.void
start()
Allows the Scheduler instance to start threads and accept tasks on them.-
Methods inherited from class io.reactivex.rxjava3.core.Scheduler
clockDriftTolerance, now, scheduleDirect, when
-
-
-
-
Field Detail
-
NONE
static final ComputationScheduler.FixedSchedulerPool NONE
This will indicate no pool is active.
-
THREAD_NAME_PREFIX
private static final java.lang.String THREAD_NAME_PREFIX
Manages a fixed number of workers.- See Also:
- Constant Field Values
-
THREAD_FACTORY
static final RxThreadFactory THREAD_FACTORY
-
KEY_MAX_THREADS
static final java.lang.String 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:
- Constant Field Values
-
MAX_THREADS
static final int MAX_THREADS
The maximum number of computation scheduler threads.
-
SHUTDOWN_WORKER
static final ComputationScheduler.PoolWorker SHUTDOWN_WORKER
-
threadFactory
final java.util.concurrent.ThreadFactory threadFactory
-
pool
final java.util.concurrent.atomic.AtomicReference<ComputationScheduler.FixedSchedulerPool> pool
-
KEY_COMPUTATION_PRIORITY
private static final java.lang.String KEY_COMPUTATION_PRIORITY
The name of the system property for setting the thread priority for this Scheduler.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ComputationScheduler
public ComputationScheduler()
Create a scheduler with pool size equal to the available processor count and using least-recent worker selection policy.
-
ComputationScheduler
public ComputationScheduler(java.util.concurrent.ThreadFactory threadFactory)
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 Detail
-
cap
static int cap(int cpuCount, int paramThreads)
-
createWorker
@NonNull public @NonNull Scheduler.Worker createWorker()
Description copied from class:Scheduler
Retrieves or creates a newScheduler.Worker
that represents sequential execution of actions.When work is completed, the
Worker
instance should be released by callingDisposable.dispose()
to avoid potential resource leaks in the underlying task-execution scheme.Work on a
Scheduler.Worker
is guaranteed to be sequential and non-overlapping.- Specified by:
createWorker
in classScheduler
- Returns:
- a Worker representing a serial queue of actions to be executed
-
createWorkers
public void createWorkers(int number, SchedulerMultiWorkerSupport.WorkerCallback callback)
Description copied from interface:SchedulerMultiWorkerSupport
Creates the given number ofScheduler.Worker
instances that are possibly backed by distinct threads and calls the specifiedConsumer
with them.- Specified by:
createWorkers
in 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 java.lang.Runnable run, long delay, java.util.concurrent.TimeUnit unit)
Description copied from class:Scheduler
Schedules 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:
scheduleDirect
in 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 java.lang.Runnable run, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
Description copied from class:Scheduler
Schedules 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:
schedulePeriodicallyDirect
in 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:Scheduler
Allows 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
RuntimeException
if it doesn't support this functionality.
-
shutdown
public void shutdown()
Description copied from class:Scheduler
Instructs the Scheduler instance to stop threads, stop accepting tasks on any outstandingScheduler.Worker
instances and clean up any associated resources with this Scheduler.Implementations should make sure the call is idempotent, thread-safe and should not throw any
RuntimeException
if it doesn't support this functionality.
-
-