Package io.servicetalk.concurrent.api
Class Executors
- java.lang.Object
-
- io.servicetalk.concurrent.api.Executors
-
public final class Executors extends java.lang.Object
Utility methods to create variousExecutor
s.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Executor
from(java.util.concurrent.Executor jdkExecutor)
Creates a newExecutor
from the providedjdkExecutor
.static Executor
from(java.util.concurrent.Executor jdkExecutor, java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
Creates a newExecutor
usingexecutor
to execute immediate tasks andscheduler
to schedule delayed tasks.static Executor
from(java.util.concurrent.Executor jdkExecutor, java.util.concurrent.ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel)
Creates a newExecutor
usingexecutor
to execute immediate tasks andscheduler
to schedule delayed tasks.static Executor
from(java.util.concurrent.ExecutorService executorService)
Creates a newExecutor
from the providedExecutorService
.static Executor
from(java.util.concurrent.ExecutorService executorService, boolean mayInterruptOnCancel)
Creates a newExecutor
from the providedExecutorService
.static Executor
from(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
Creates a newExecutor
from the providedScheduledExecutorService
.static Executor
from(java.util.concurrent.ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel)
Creates a newExecutor
from the providedScheduledExecutorService
.static Executor
immediate()
Returns anExecutor
that executes all tasks submitted viaExecutor.execute(Runnable)
immediately by callingRunnable.run()
on the calling thread.static Executor
newCachedThreadExecutor()
Creates a newExecutor
that creates as many threads as required but reuses threads when possible.static Executor
newCachedThreadExecutor(java.util.concurrent.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, java.util.concurrent.ThreadFactory threadFactory)
Creates a newExecutor
that has a fixed number of threads as specified by thesize
.
-
-
-
Method Detail
-
immediate
public static Executor 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.- Returns:
- An
Executor
that executes all tasks submitted viaExecutor.execute(Runnable)
immediately on the calling thread.
-
newFixedSizeExecutor
public static Executor newFixedSizeExecutor(int size)
Creates a newExecutor
that has a fixed number of threads as specified by thesize
.
-
newFixedSizeExecutor
public static Executor newFixedSizeExecutor(int size, java.util.concurrent.ThreadFactory threadFactory)
Creates a newExecutor
that has a fixed number of threads as specified by thesize
.
-
newCachedThreadExecutor
public static Executor newCachedThreadExecutor()
Creates a newExecutor
that creates as many threads as required but reuses threads when possible.- Returns:
- A new
Executor
.
-
newCachedThreadExecutor
public static Executor newCachedThreadExecutor(java.util.concurrent.ThreadFactory threadFactory)
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
public static Executor from(java.util.concurrent.Executor jdkExecutor)
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
public static Executor from(java.util.concurrent.ExecutorService executorService)
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
public static Executor from(java.util.concurrent.ExecutorService executorService, boolean mayInterruptOnCancel)
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
public static Executor from(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
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(java.util.concurrent.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(java.util.concurrent.Executor jdkExecutor, java.util.concurrent.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(java.util.concurrent.Executor jdkExecutor, java.util.concurrent.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
.
-
-