Class Executors
Executor
s.-
Method Summary
Modifier and TypeMethodDescriptionstatic Executor
Creates a newExecutor
from the providedjdkExecutor
.static Executor
from
(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService) Creates a newExecutor
usingexecutor
to execute immediate tasks andscheduler
to schedule delayed tasks.static Executor
from
(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutor
usingexecutor
to execute immediate tasks andscheduler
to schedule delayed tasks.static Executor
from
(ExecutorService executorService) Creates a newExecutor
from the providedExecutorService
.static Executor
from
(ExecutorService executorService, boolean mayInterruptOnCancel) Creates a newExecutor
from the providedExecutorService
.static Executor
from
(ScheduledExecutorService scheduledExecutorService) Creates a newExecutor
from the providedScheduledExecutorService
.static Executor
from
(ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutor
from the providedScheduledExecutorService
.static Executor
global()
Returns a globalExecutor
instance, which should be used for operations that do not involve I/O and are expected to happen concurrently.static Executor
Returns anExecutor
that executes all tasks submitted viaExecutor.execute(Runnable)
immediately by callingRunnable.run()
on the calling thread.static Executor
Creates a newExecutor
that creates as many threads as required but reuses threads when possible.static Executor
newCachedThreadExecutor
(ThreadFactory threadFactory) Creates a newExecutor
that creates as many threads as required but reuses threads when possible.static Executor
newFixedSizeExecutor
(int size) Creates a newExecutor
that has a fixed number of threads as specified by thesize
.static Executor
newFixedSizeExecutor
(int size, ThreadFactory threadFactory) Creates a newExecutor
that has a fixed number of threads as specified by thesize
.
-
Method Details
-
immediate
Returns anExecutor
that executes all tasks submitted viaExecutor.execute(Runnable)
immediately by callingRunnable.run()
on the calling thread.Executor.schedule(Runnable, long, TimeUnit)
will use a global scheduler.The lifecycle of this instance shouldn't need to be managed by the user. The returned instance of
Executor
must not be closed.- Returns:
- An
Executor
that executes all tasks submitted viaExecutor.execute(Runnable)
immediately on the calling thread.
-
global
Returns a globalExecutor
instance, which should be used for operations that do not involve I/O and are expected to happen concurrently. It creates as many threads as required but reuses threads when possible. It is therefore 'safe to block' when using it.The lifecycle of this instance shouldn't need to be managed by the user. The returned instance of
Executor
must not be closed.- Returns:
- An
Executor
which serves as a global mechanism for executing concurrent operations.
-
newFixedSizeExecutor
Creates a newExecutor
that has a fixed number of threads as specified by thesize
. -
newFixedSizeExecutor
Creates a newExecutor
that has a fixed number of threads as specified by thesize
.- Parameters:
size
- Number of threads used by the newly createdExecutor
.threadFactory
-ThreadFactory
to use.- Returns:
- A new
Executor
that will use thesize
number of threads.
-
newCachedThreadExecutor
Creates a newExecutor
that creates as many threads as required but reuses threads when possible.- Returns:
- A new
Executor
.
-
newCachedThreadExecutor
Creates a newExecutor
that creates as many threads as required but reuses threads when possible.- Parameters:
threadFactory
-ThreadFactory
to use.- Returns:
- A new
Executor
.
-
from
Creates a newExecutor
from the providedjdkExecutor
.Delayed task execution will be delegated to a global scheduler, unless passed
Executor
is an instance ofScheduledExecutorService
.Task execution will not honor cancellations unless passed
Executor
is an instance ofExecutorService
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
jdkExecutor
-Executor
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
(if possible).- Returns:
Executor
that wraps the passedjdkExecutor
.
-
from
Creates a newExecutor
from the providedExecutorService
.Delayed task execution will be delegated to a global scheduler, unless passed
ExecutorService
is an instance ofScheduledExecutorService
.When a running task is cancelled, the thread running it will be interrupted. For overriding this behavior use
from(ExecutorService, boolean)
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
executorService
-ExecutorService
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
.- Returns:
Executor
that wraps the passedexecutorService
.
-
from
Creates a newExecutor
from the providedExecutorService
. Delayed task execution will be delegated to a global scheduler, unless passedExecutorService
is an instance ofScheduledExecutorService
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
executorService
-ExecutorService
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
.mayInterruptOnCancel
- If set totrue
, when a task is cancelled, thread running the task will be interrupted.- Returns:
Executor
that wraps the passedexecutorService
.
-
from
Creates a newExecutor
from the providedScheduledExecutorService
. When a running task is cancelled, the thread running it will be interrupted. For overriding this behavior usefrom(ScheduledExecutorService, boolean)
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
scheduledExecutorService
-ScheduledExecutorService
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
.- Returns:
Executor
that wraps the passedscheduledExecutorService
.
-
from
public static Executor from(ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutor
from the providedScheduledExecutorService
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
scheduledExecutorService
-ScheduledExecutorService
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
.mayInterruptOnCancel
- If set totrue
, when a task is cancelled, thread running the task will be interrupted.- Returns:
Executor
that wraps the passedscheduledExecutorService
.
-
from
public static Executor from(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService) Creates a newExecutor
usingexecutor
to execute immediate tasks andscheduler
to schedule delayed tasks. When a running task is cancelled, the thread running it will be interrupted. For overriding this behavior usefrom(java.util.concurrent.Executor, ScheduledExecutorService, boolean)
. Task execution will not honor cancellations unless passedExecutor
is an instance ofExecutorService
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
jdkExecutor
-Executor
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
(if possible).scheduledExecutorService
-ScheduledExecutorService
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
.- Returns:
- A new
Executor
.
-
from
public static Executor from(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutor
usingexecutor
to execute immediate tasks andscheduler
to schedule delayed tasks. Task execution will not honor cancellations unless passedExecutor
is an instance ofExecutorService
.Long running tasks
Executor
implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutor
instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor
.- Parameters:
jdkExecutor
-Executor
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
(if possible).scheduledExecutorService
-ScheduledExecutorService
to use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()
will callExecutorService.shutdown()
.mayInterruptOnCancel
- If set totrue
, when a task is cancelled, thread running the task will be interrupted.- Returns:
- A new
Executor
.
-