Class Executors
Executors.-
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorCreates a newExecutorfrom the providedjdkExecutor.static Executorfrom(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService) Creates a newExecutorusingexecutorto execute immediate tasks andschedulerto schedule delayed tasks.static Executorfrom(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutorusingexecutorto execute immediate tasks andschedulerto schedule delayed tasks.static Executorfrom(ExecutorService executorService) Creates a newExecutorfrom the providedExecutorService.static Executorfrom(ExecutorService executorService, boolean mayInterruptOnCancel) Creates a newExecutorfrom the providedExecutorService.static Executorfrom(ScheduledExecutorService scheduledExecutorService) Creates a newExecutorfrom the providedScheduledExecutorService.static Executorfrom(ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutorfrom the providedScheduledExecutorService.static Executorglobal()Returns a globalExecutorinstance, which should be used for operations that do not involve I/O and are expected to happen concurrently.static ExecutorReturns anExecutorthat executes all tasks submitted viaExecutor.execute(Runnable)immediately by callingRunnable.run()on the calling thread.static ExecutorCreates a newExecutorthat creates as many threads as required but reuses threads when possible.static ExecutornewCachedThreadExecutor(ThreadFactory threadFactory) Creates a newExecutorthat creates as many threads as required but reuses threads when possible.static ExecutornewFixedSizeExecutor(int size) Creates a newExecutorthat has a fixed number of threads as specified by thesize.static ExecutornewFixedSizeExecutor(int size, ThreadFactory threadFactory) Creates a newExecutorthat has a fixed number of threads as specified by thesize.
-
Method Details
-
immediate
Returns anExecutorthat 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
Executormust not be closed.- Returns:
- An
Executorthat executes all tasks submitted viaExecutor.execute(Runnable)immediately on the calling thread.
-
global
Returns a globalExecutorinstance, 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
Executormust not be closed.- Returns:
- An
Executorwhich serves as a global mechanism for executing concurrent operations.
-
newFixedSizeExecutor
Creates a newExecutorthat has a fixed number of threads as specified by thesize. -
newFixedSizeExecutor
Creates a newExecutorthat has a fixed number of threads as specified by thesize.- Parameters:
size- Number of threads used by the newly createdExecutor.threadFactory-ThreadFactoryto use.- Returns:
- A new
Executorthat will use thesizenumber of threads.
-
newCachedThreadExecutor
Creates a newExecutorthat creates as many threads as required but reuses threads when possible.- Returns:
- A new
Executor.
-
newCachedThreadExecutor
Creates a newExecutorthat creates as many threads as required but reuses threads when possible.- Parameters:
threadFactory-ThreadFactoryto use.- Returns:
- A new
Executor.
-
from
Creates a newExecutorfrom the providedjdkExecutor.Delayed task execution will be delegated to a global scheduler, unless passed
Executoris an instance ofScheduledExecutorService.Task execution will not honor cancellations unless passed
Executoris an instance ofExecutorService.Long running tasks
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
jdkExecutor-Executorto 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:
Executorthat wraps the passedjdkExecutor.
-
from
Creates a newExecutorfrom the providedExecutorService.Delayed task execution will be delegated to a global scheduler, unless passed
ExecutorServiceis 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
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
executorService-ExecutorServiceto use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()will callExecutorService.shutdown().- Returns:
Executorthat wraps the passedexecutorService.
-
from
Creates a newExecutorfrom the providedExecutorService. Delayed task execution will be delegated to a global scheduler, unless passedExecutorServiceis an instance ofScheduledExecutorService.Long running tasks
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
executorService-ExecutorServiceto 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:
Executorthat wraps the passedexecutorService.
-
from
Creates a newExecutorfrom 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
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
scheduledExecutorService-ScheduledExecutorServiceto use for executing tasks. The lifetime of this object is transferred to the return value. In other wordsAsyncCloseable.closeAsync()will callExecutorService.shutdown().- Returns:
Executorthat wraps the passedscheduledExecutorService.
-
from
public static Executor from(ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel) Creates a newExecutorfrom the providedScheduledExecutorService.Long running tasks
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
scheduledExecutorService-ScheduledExecutorServiceto 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:
Executorthat wraps the passedscheduledExecutorService.
-
from
public static Executor from(Executor jdkExecutor, ScheduledExecutorService scheduledExecutorService) Creates a newExecutorusingexecutorto execute immediate tasks andschedulerto 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 passedExecutoris an instance ofExecutorService.Long running tasks
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
jdkExecutor-Executorto 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-ScheduledExecutorServiceto 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 newExecutorusingexecutorto execute immediate tasks andschedulerto schedule delayed tasks. Task execution will not honor cancellations unless passedExecutoris an instance ofExecutorService.Long running tasks
Executorimplementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the sameExecutorinstance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in theExecutor.- Parameters:
jdkExecutor-Executorto 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-ScheduledExecutorServiceto 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.
-