java.lang.Object | |
↳ | io.reactivex.schedulers.Schedulers |
Static factory methods for returning standard Scheduler instances.
The initial and runtime values of the various scheduler types can be overridden via the
RxJavaPlugins.setInit(scheduler name)SchedulerHandler()
and
RxJavaPlugins.set(scheduler name)SchedulerHandler()
respectively.
Supported system properties (System.getProperty()
):
rx2.io-priority
(int): sets the thread priority of the io()
Scheduler, default is NORM_PRIORITY
rx2.computation-threads
(int): sets the number of threads in the computation()
Scheduler, default is the number of available CPUsrx2.computation-priority
(int): sets the thread priority of the computation()
Scheduler, default is NORM_PRIORITY
rx2.newthread-priority
(int): sets the thread priority of the newThread()
Scheduler, default is NORM_PRIORITY
rx2.single-priority
(int): sets the thread priority of the single()
Scheduler, default is NORM_PRIORITY
rx2.purge-enabled
(boolean): enables periodic purging of all Scheduler's backing thread pools, default is falserx2.purge-period-seconds
(int): specifies the periodic purge interval of all Scheduler's backing thread pools, default is 1 secondPublic Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static Scheduler |
computation()
Returns a default, shared
Scheduler instance intended for computational work. | ||||||||||
static Scheduler |
from(Executor executor)
Wraps an Executor into a new Scheduler instance and delegates
schedule()
calls to it. | ||||||||||
static Scheduler |
io()
Returns a default, shared
Scheduler instance intended for IO-bound work. | ||||||||||
static Scheduler | newThread() | ||||||||||
static void |
shutdown()
Shuts down the standard Schedulers.
| ||||||||||
static Scheduler |
single()
Returns a default, shared, single-thread-backed
Scheduler instance for work
requiring strongly-sequential execution on the same background thread. | ||||||||||
static void |
start()
Starts the standard Schedulers.
| ||||||||||
static Scheduler |
trampoline()
Returns a default, shared
Scheduler instance whose Scheduler.Worker
instances queue work and execute them in a FIFO manner on one of the participating threads. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Returns a default, shared Scheduler
instance intended for computational work.
This can be used for event-loops, processing callbacks and other computational work.
It is not recommended to perform blocking, IO-bound work on this scheduler. Use io()
instead.
The default instance has a backing pool of single-threaded ScheduledExecutorService instances equal to
the number of available processors (availableProcessors()
) to the Java VM.
Unhandled errors will be delivered to the scheduler Thread's java.lang.Thread.UncaughtExceptionHandler.
This type of scheduler is less sensitive to leaking Scheduler.Worker
instances, although
not disposing a worker that has timed/delayed tasks not cancelled by other means may leak resources and/or
execute those tasks "unexpectedly".
If the setFailOnNonBlockingScheduler(boolean)
is set to true, attempting to execute
operators that block while running on this scheduler will throw an IllegalStateException
.
You can control certain properties of this standard scheduler via system properties that have to be set
before the Schedulers
class is referenced in your code.
Supported system properties (System.getProperty()
):
rx2.computation-threads
(int): sets the number of threads in the computation()
Scheduler, default is the number of available CPUsrx2.computation-priority
(int): sets the thread priority of the computation()
Scheduler, default is NORM_PRIORITY
The default value of this scheduler can be overridden at initialization time via the
setInitComputationSchedulerHandler(io.reactivex.functions.Function)
plugin method.
Note that due to possible initialization cycles, using any of the other scheduler-returning methods will
result in a NullPointerException
.
Once the Schedulers
class has been initialized, you can override the returned Scheduler
instance
via the setComputationSchedulerHandler(io.reactivex.functions.Function)
method.
It is possible to create a fresh instance of this scheduler with a custom ThreadFactory, via the
createComputationScheduler(ThreadFactory)
method. Note that such custom
instances require a manual call to shutdown()
to allow the JVM to exit or the
(J2EE) container to unload properly.
Operators on the base reactive classes that use this scheduler are marked with the
@SchedulerSupport
(COMPUTATION
)
annotation.
Scheduler
meant for computation-bound work
Wraps an Executor into a new Scheduler instance and delegates schedule()
calls to it.
If the provided executor doesn't support any of the more specific standard Java executor
APIs, cancelling tasks scheduled by this scheduler can't be interrupted when they are
executing but only prevented from running prior to that. In addition, tasks scheduled with
a time delay or periodically will use the single()
scheduler for the timed waiting
before posting the actual task to the given executor.
If the provided executor supports the standard Java ExecutorService API,
cancelling tasks scheduled by this scheduler can be cancelled/interrupted by calling
dispose()
. In addition, tasks scheduled with
a time delay or periodically will use the single()
scheduler for the timed waiting
before posting the actual task to the given executor.
If the provided executor supports the standard Java ScheduledExecutorService API,
cancelling tasks scheduled by this scheduler can be cancelled/interrupted by calling
dispose()
. In addition, tasks scheduled with
a time delay or periodically will use the provided executor. Note, however, if the provided
ScheduledExecutorService
instance is not single threaded, tasks scheduled
with a time delay close to each other may end up executing in different order than
the original schedule() call was issued. This limitation may be lifted in a future patch.
Starting, stopping and restarting this scheduler is not supported (no-op) and the provided executor's lifecycle must be managed externally:
ExecutorService exec = Executors.newSingleThreadedExecutor();
try {
Scheduler scheduler = Schedulers.from(exec);
Flowable.just(1)
.subscribeOn(scheduler)
.map(v -> v + 1)
.observeOn(scheduler)
.blockingSubscribe(System.out::println);
} finally {
exec.shutdown();
}
This type of scheduler is less sensitive to leaking Scheduler.Worker
instances, although
not disposing a worker that has timed/delayed tasks not cancelled by other means may leak resources and/or
execute those tasks "unexpectedly".
Note that this method returns a new Scheduler
instance, even for the same Executor instance.
executor | the executor to wrap |
---|
Returns a default, shared Scheduler
instance intended for IO-bound work.
This can be used for asynchronously performing blocking IO.
The implementation is backed by a pool of single-threaded ScheduledExecutorService instances
that will try to reuse previoulsy started instances used by the worker
returned by createWorker()
but otherwise will start a new backing
ScheduledExecutorService instance. Note that this scheduler may create an unbounded number
of worker threads that can result in system slowdowns or OutOfMemoryError
. Therefore, for casual uses
or when implementing an operator, the Worker instances must be disposed via dispose()
.
It is not recommended to perform computational work on this scheduler. Use computation()
instead.
Unhandled errors will be delivered to the scheduler Thread's java.lang.Thread.UncaughtExceptionHandler.
You can control certain properties of this standard scheduler via system properties that have to be set
before the Schedulers
class is referenced in your code.
Supported system properties (System.getProperty()
):
rx2.io-priority
(int): sets the thread priority of the io()
Scheduler, default is NORM_PRIORITY
The default value of this scheduler can be overridden at initialization time via the
setInitIoSchedulerHandler(io.reactivex.functions.Function)
plugin method.
Note that due to possible initialization cycles, using any of the other scheduler-returning methods will
result in a NullPointerException
.
Once the Schedulers
class has been initialized, you can override the returned Scheduler
instance
via the setIoSchedulerHandler(io.reactivex.functions.Function)
method.
It is possible to create a fresh instance of this scheduler with a custom ThreadFactory, via the
createIoScheduler(ThreadFactory)
method. Note that such custom
instances require a manual call to shutdown()
to allow the JVM to exit or the
(J2EE) container to unload properly.
Operators on the base reactive classes that use this scheduler are marked with the
@SchedulerSupport
(IO
)
annotation.
Scheduler
meant for IO-bound work
Returns a default, shared Scheduler
instance that creates a new Thread
for each unit of work.
The default implementation of this scheduler creates a new, single-threaded ScheduledExecutorService for
each invocation of the scheduleDirect(Runnable)
(plus its overloads) and createWorker()
methods, thus an unbounded number of worker threads may be created that can
result in system slowdowns or OutOfMemoryError
. Therefore, for casual uses or when implementing an operator,
the Worker instances must be disposed via dispose()
.
Unhandled errors will be delivered to the scheduler Thread's java.lang.Thread.UncaughtExceptionHandler.
You can control certain properties of this standard scheduler via system properties that have to be set
before the Schedulers
class is referenced in your code.
Supported system properties (System.getProperty()
):
rx2.newthread-priority
(int): sets the thread priority of the newThread()
Scheduler, default is NORM_PRIORITY
The default value of this scheduler can be overridden at initialization time via the
setInitNewThreadSchedulerHandler(io.reactivex.functions.Function)
plugin method.
Note that due to possible initialization cycles, using any of the other scheduler-returning methods will
result in a NullPointerException
.
Once the Schedulers
class has been initialized, you can override the returned Scheduler
instance
via the setNewThreadSchedulerHandler(io.reactivex.functions.Function)
method.
It is possible to create a fresh instance of this scheduler with a custom ThreadFactory, via the
createNewThreadScheduler(ThreadFactory)
method. Note that such custom
instances require a manual call to shutdown()
to allow the JVM to exit or the
(J2EE) container to unload properly.
Operators on the base reactive classes that use this scheduler are marked with the
@SchedulerSupport
(NEW_TRHEAD
)
annotation.
Scheduler
that creates new threads
Shuts down the standard Schedulers.
The operation is idempotent and thread-safe.
Returns a default, shared, single-thread-backed Scheduler
instance for work
requiring strongly-sequential execution on the same background thread.
Uses:
Unhandled errors will be delivered to the scheduler Thread's java.lang.Thread.UncaughtExceptionHandler.
This type of scheduler is less sensitive to leaking Scheduler.Worker
instances, although
not disposing a worker that has timed/delayed tasks not cancelled by other means may leak resources and/or
execute those tasks "unexpectedly".
If the setFailOnNonBlockingScheduler(boolean)
is set to true, attempting to execute
operators that block while running on this scheduler will throw an IllegalStateException
.
You can control certain properties of this standard scheduler via system properties that have to be set
before the Schedulers
class is referenced in your code.
Supported system properties (System.getProperty()
):
rx2.single-priority
(int): sets the thread priority of the single()
Scheduler, default is NORM_PRIORITY
The default value of this scheduler can be overridden at initialization time via the
setInitSingleSchedulerHandler(io.reactivex.functions.Function)
plugin method.
Note that due to possible initialization cycles, using any of the other scheduler-returning methods will
result in a NullPointerException
.
Once the Schedulers
class has been initialized, you can override the returned Scheduler
instance
via the setSingleSchedulerHandler(io.reactivex.functions.Function)
method.
It is possible to create a fresh instance of this scheduler with a custom ThreadFactory, via the
createSingleScheduler(ThreadFactory)
method. Note that such custom
instances require a manual call to shutdown()
to allow the JVM to exit or the
(J2EE) container to unload properly.
Operators on the base reactive classes that use this scheduler are marked with the
@SchedulerSupport
(SINGLE
)
annotation.
Scheduler
that shares a single backing thread.Starts the standard Schedulers.
The operation is idempotent and thread-safe.
Returns a default, shared Scheduler
instance whose Scheduler.Worker
instances queue work and execute them in a FIFO manner on one of the participating threads.
The default implementation's scheduleDirect(Runnable)
methods execute the tasks on the current thread
without any queueing and the timed overloads use blocking sleep as well.
Note that this scheduler can't be reliably used to return the execution of tasks to the "main" thread. Such behavior requires a blocking-queueing scheduler currently not provided by RxJava itself but may be found in external libraries.
This scheduler can't be overridden via an RxJavaPlugins
method.
Scheduler
that queues work on the current thread