public class Schedulers
extends java.lang.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 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(java.util.concurrent.Executor executor)
Create a
Scheduler which uses a backing Executor to schedule
Runnables for async operators. |
static Scheduler |
fromExecutor(java.util.concurrent.Executor executor,
boolean trampoline)
Create a
Scheduler which uses a backing Executor to schedule
Runnables for async operators. |
static Scheduler |
fromExecutorService(java.util.concurrent.ExecutorService executorService)
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,
java.util.concurrent.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(java.lang.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(java.lang.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(java.lang.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,
java.util.concurrent.ThreadFactory threadFactory)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(java.lang.String name)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(java.lang.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(java.lang.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(java.lang.String name)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
newSingle(java.lang.String name,
boolean daemon)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
newSingle(java.util.concurrent.ThreadFactory threadFactory)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static TimedScheduler |
newTimer(java.lang.String name)
Create a new
TimedScheduler backed by a single threaded ScheduledExecutorService . |
static TimedScheduler |
newTimer(java.lang.String name,
boolean daemon)
Create a new
TimedScheduler backed by a single threaded ScheduledExecutorService . |
static TimedScheduler |
newTimer(java.util.concurrent.ThreadFactory threadFactory)
Create a new
TimedScheduler backed by a single threaded ScheduledExecutorService . |
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 |
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 shutdown 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()
Create or reuse a hash-wheel based
TimedScheduler with a resolution of 50MS
All times will rounded up to the closest multiple of this resolution. |
public static final int DEFAULT_POOL_SIZE
Runtime.availableProcessors()
public static Scheduler fromExecutor(java.util.concurrent.Executor executor)
Scheduler
which uses a backing Executor
to schedule
Runnables for async operators.executor
- an Executor
Scheduler
public static Scheduler fromExecutor(java.util.concurrent.Executor executor, boolean trampoline)
Scheduler
which uses a backing Executor
to schedule
Runnables for async operators.public static Scheduler fromExecutorService(java.util.concurrent.ExecutorService executorService)
Scheduler
which uses a backing ExecutorService
to schedule
Runnables for async operators.executorService
- an ExecutorService
Scheduler
public 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()
Scheduler
public static Scheduler newElastic(java.lang.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(java.lang.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.Worker
Scheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newElastic(java.lang.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.Worker
daemon
- false if the Scheduler
requires an explicit Scheduler.shutdown()
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, java.util.concurrent.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.Worker
threadFactory
- 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(java.lang.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(java.lang.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(java.lang.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.shutdown()
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, java.util.concurrent.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 Thread
Scheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newSingle(java.lang.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(java.lang.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.shutdown()
to exit the VM.Scheduler
that hosts a single-threaded ExecutorService-based
workerpublic static Scheduler newSingle(java.util.concurrent.ThreadFactory threadFactory)
Scheduler
that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work.public static TimedScheduler newTimer(java.lang.String name)
TimedScheduler
backed by a single threaded ScheduledExecutorService
.name
- timer thread prefixTimedScheduler
public static TimedScheduler newTimer(java.lang.String name, boolean daemon)
TimedScheduler
backed by a single threaded ScheduledExecutorService
.name
- Component and thread name prefixdaemon
- false if the Scheduler
requires an explicit Scheduler.shutdown()
to exit the VM.TimedScheduler
public static TimedScheduler newTimer(java.util.concurrent.ThreadFactory threadFactory)
TimedScheduler
backed by a single threaded ScheduledExecutorService
.threadFactory
- a ThreadFactory
to use for the unique thread of the
TimedScheduler
TimedScheduler
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()
Schedulers
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.
factoryInstance
- an arbitrary Schedulers.Factory
instance.public static void shutdownNow()
Scheduler
and call shutdown 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 single()
calls until shutdown.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.shutdown()
to release the wrapped worker.
original
- a Scheduler
to call upon to get the single Scheduler.Worker
Scheduler
consistently returning a same worker from a
source Scheduler
public static TimedScheduler timer()
TimedScheduler
with a resolution of 50MS
All times will rounded up to the closest multiple of this resolution.TimedScheduler