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.
  • Field Details

  • 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

      public ComputationScheduler(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 Details

    • cap

      static int cap(int cpuCount, int paramThreads)
    • createWorker

      @NonNull public @NonNull Scheduler.Worker createWorker()
      Description copied from class: Scheduler
      Retrieves or creates a new Scheduler.Worker that represents sequential execution of actions.

      When work is completed, the Worker instance should be released by calling Disposable.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 class Scheduler
      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 of Scheduler.Worker instances that are possibly backed by distinct threads and calls the specified Consumer with them.
      Specified by:
      createWorkers in interface SchedulerMultiWorkerSupport
      Parameters:
      number - the number of workers to create, positive
      callback - 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: 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 class Scheduler
      Parameters:
      run - the task to schedule
      delay - the delay amount, non-positive values indicate non-delayed scheduling
      unit - 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: 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 after initialDelay + period, the third after initialDelay + 2 * period, and so on.

      Overrides:
      schedulePeriodicallyDirect in class Scheduler
      Parameters:
      run - the task to schedule
      initialDelay - the initial delay amount, non-positive values indicate non-delayed scheduling
      period - the period at which the task should be re-executed
      unit - 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.

      Overrides:
      start in class Scheduler
    • shutdown

      public void shutdown()
      Description copied from class: Scheduler
      Instructs the Scheduler instance to stop threads, stop accepting tasks on any outstanding Scheduler.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.

      Overrides:
      shutdown in class Scheduler