public abstract class Schedulers extends Object
Schedulers provide various Scheduler generator useable by publishOn or subscribeOn :
fromExecutorService(ExecutorService)}. newParallel(java.lang.String)
: Optimized for fast Runnable executions single() : Optimized
for low-latency Runnable executions immediate(). | Modifier and Type | Class and Description |
|---|---|
static interface |
Schedulers.Factory
Public factory hook to override Schedulers behavior globally
|
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_POOL_SIZE
Default number of processors available to the runtime on init (min 4)
|
| Constructor and Description |
|---|
Schedulers() |
| Modifier and Type | Method and Description |
|---|---|
static ExecutorService |
decorateExecutorService(String schedulerType,
Supplier<? extends ExecutorService> actual) |
static ScheduledExecutorService |
decorateScheduledExecutorService(String schedulerType,
Supplier<? extends ScheduledExecutorService> actual) |
static Scheduler |
elastic()
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
fromExecutor(Executor executor)
|
static Scheduler |
fromExecutor(Executor executor,
boolean trampoline)
|
static Scheduler |
fromExecutorService(ExecutorService executorService)
Create a
Scheduler which uses a backing ExecutorService to schedule
Runnables for async operators. |
static Scheduler |
fromExecutorService(ExecutorService executorService,
boolean interruptOnCancel)
Create a
Scheduler which uses a backing ExecutorService to schedule
Runnables for async operators. |
static Scheduler |
immediate()
Executes tasks on the caller's thread immediately.
|
static Scheduler |
newElastic(int ttlSeconds,
ThreadFactory threadFactory)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(String name)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(String name,
int ttlSeconds)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(String name,
int ttlSeconds,
boolean daemon)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newParallel(int parallelism,
ThreadFactory threadFactory)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(String name)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(String name,
int parallelism)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(String name,
int parallelism,
boolean daemon)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newSingle(String name)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
newSingle(String name,
boolean daemon)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
newSingle(ThreadFactory threadFactory)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static TimedScheduler |
newTimer(String name)
Deprecated.
in 3.0.6 and from 3.1 onwards, all Scheduler support time.
newSingle(String) is a direct equivalent if required. |
static TimedScheduler |
newTimer(String name,
boolean daemon)
Deprecated.
in 3.0.6 and from 3.1 onwards, all Scheduler support time.
newSingle(String, boolean) is a direct equivalent if required. |
static TimedScheduler |
newTimer(ThreadFactory threadFactory)
Deprecated.
in 3.0.6 and from 3.1 onwards, all Scheduler support time.
newSingle(ThreadFactory) is a direct equivalent if required. |
static void |
onHandleError(BiConsumer<Thread,? super Throwable> c)
Define a hook that is executed when a
Scheduler has
handled an error. |
static Scheduler |
parallel()
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static void |
resetFactory()
Re-apply default factory to
Schedulers |
static void |
resetOnHandleError()
Reset the
onHandleError(BiConsumer) hook to the default no-op behavior. |
static void |
setFactory(Schedulers.Factory factoryInstance)
Override
Schedulers finite signatures of newParallel(java.lang.String), newSingle(java.lang.String), newTimer(java.lang.String) and newElastic(java.lang.String) factory using the matching
method signature in the target class. |
static void |
shutdownNow()
Clear any cached
Scheduler and call dispose on them. |
static Scheduler |
single()
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
single(Scheduler original)
Wraps a single worker of some other
Scheduler and provides Scheduler.Worker services on top of it. |
static TimedScheduler |
timer()
Deprecated.
in 3.0.6 and from 3.1 onwards, all Scheduler support time.
single() is a direct equivalent if required. |
public static final int DEFAULT_POOL_SIZE
Runtime.availableProcessors()public static Scheduler fromExecutorService(ExecutorService executorService)
Scheduler which uses a backing ExecutorService to schedule
Runnables for async operators.executorService - an ExecutorServiceSchedulerpublic static Scheduler fromExecutorService(ExecutorService executorService, boolean interruptOnCancel)
Scheduler which uses a backing ExecutorService to schedule
Runnables for async operators.executorService - an ExecutorServiceinterruptOnCancel - delegate to future.cancel(true) on Disposable.dispose()Schedulerpublic static Scheduler elastic()
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
The default time-to-live for unused thread pools is 60 seconds, use the appropriate factory to set a different value.
This scheduler is not restartable.
Scheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler immediate()
Schedulerpublic static Scheduler newElastic(String name)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
The default time-to-live for unused thread pools is 60 seconds, use the appropriate factory to set a different value.
This scheduler is not restartable.
name - Thread prefixScheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newElastic(String name, int ttlSeconds)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
This scheduler is not restartable.
name - Thread prefixttlSeconds - Time-to-live for an idle Scheduler.WorkerScheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newElastic(String name, int ttlSeconds, boolean daemon)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
This scheduler is not restartable.
name - Thread prefixttlSeconds - Time-to-live for an idle Scheduler.Workerdaemon - false if the Scheduler requires an explicit Scheduler.dispose() to exit the VM.Scheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newElastic(int ttlSeconds, ThreadFactory threadFactory)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
This scheduler is not restartable.
ttlSeconds - Time-to-live for an idle Scheduler.WorkerthreadFactory - a ThreadFactory to use each thread initializationScheduler that dynamically creates ExecutorService-based
Workers and caches the thread pools, reusing them once the Workers have been shut
down.public static Scheduler newParallel(String name)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.name - Thread prefixScheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newParallel(String name, int parallelism)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.name - Thread prefixparallelism - Number of pooled workers.Scheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newParallel(String name, int parallelism, boolean daemon)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.name - Thread prefixparallelism - Number of pooled workers.daemon - false if the Scheduler requires an explicit Scheduler.dispose() to exit the VM.Scheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newParallel(int parallelism, ThreadFactory threadFactory)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.parallelism - Number of pooled workers.threadFactory - a ThreadFactory to use for the fixed initialized
number of ThreadScheduler that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newSingle(String name)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work.name - Component and thread name prefixScheduler that hosts a single-threaded ExecutorService-based
workerpublic static Scheduler newSingle(String name, boolean daemon)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work.name - Component and thread name prefixdaemon - false if the Scheduler requires an explicit Scheduler.dispose() to exit the VM.Scheduler that hosts a single-threaded ExecutorService-based
workerpublic static Scheduler newSingle(ThreadFactory threadFactory)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work.threadFactory - a ThreadFactory to use for the unique thread of the
SchedulerScheduler that hosts a single-threaded ExecutorService-based
worker@Deprecated public static TimedScheduler newTimer(String name)
newSingle(String) is a direct equivalent if required.Scheduler backed by a single threaded ScheduledExecutorService, making it guaranteed to be capable
of scheduling tasks in the future.name - timer thread prefixScheduler@Deprecated public static TimedScheduler newTimer(String name, boolean daemon)
newSingle(String, boolean) is a direct equivalent if required.Scheduler backed by a single threaded ScheduledExecutorService, making it guaranteed to be capable
of scheduling tasks in the future.name - Component and thread name prefixdaemon - false if the Scheduler requires an explicit Scheduler.dispose() to exit the VM.Scheduler@Deprecated public static TimedScheduler newTimer(ThreadFactory threadFactory)
newSingle(ThreadFactory) is a direct equivalent if required.Scheduler backed by a single threaded ScheduledExecutorService, making it guaranteed to be capable
of scheduling tasks in the future.threadFactory - a ThreadFactory to use for the unique thread of the
SchedulerSchedulerpublic static void onHandleError(BiConsumer<Thread,? super Throwable> c)
Scheduler has
handled an error. Note that it is executed after
the error has been passed to the thread uncaughtErrorHandler, which is not the
case when a fatal error occurs (see Exceptions.throwIfJvmFatal(Throwable)).c - the new hook to set, or null to ignore (default).public static Scheduler parallel()
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.Scheduler that hosts a fixed pool of single-threaded
ExecutorService-based workerspublic static void resetFactory()
Schedulerspublic static void resetOnHandleError()
onHandleError(BiConsumer) hook to the default no-op behavior.public static void setFactory(Schedulers.Factory factoryInstance)
Schedulers finite signatures of newParallel(java.lang.String), newSingle(java.lang.String), newTimer(java.lang.String) and newElastic(java.lang.String) factory using the matching
method signature in the target class. A finite signature corresponds to those
including a ThreadFactory argument and should be instance methods.
This method should be called safely and with caution, typically on app startup.
Note that cached schedulers (like single()) are also shut down and will be
re-created from the new factory upon next use.
factoryInstance - an arbitrary Schedulers.Factory instance.public static void shutdownNow()
Scheduler and call dispose on them.public static Scheduler single()
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. Will cache the returned schedulers for subsequent calls until dispose.Scheduler that hosts a single-threaded
ExecutorService-based workerpublic static Scheduler single(Scheduler original)
Scheduler and provides Scheduler.Worker services on top of it.
Use the Scheduler.dispose() to release the wrapped worker.
original - a Scheduler to call upon to get the single Scheduler.WorkerScheduler consistently returning a same worker from a
source Scheduler@Deprecated public static TimedScheduler timer()
single() is a direct equivalent if required.Scheduler.Schedulerpublic static ExecutorService decorateExecutorService(String schedulerType, Supplier<? extends ExecutorService> actual)
public static ScheduledExecutorService decorateScheduledExecutorService(String schedulerType, Supplier<? extends ScheduledExecutorService> actual)