Package host.anzo.core.service
Class ThreadPoolService
java.lang.Object
host.anzo.core.service.ThreadPoolService
- All Implemented Interfaces:
IMetric,host.anzo.commons.interfaces.startup.IShutdownable
public class ThreadPoolService
extends Object
implements host.anzo.commons.interfaces.startup.IShutdownable, IMetric
- Since:
- 8/19/2021
-
Method Summary
Modifier and TypeMethodDescriptionvoidExecutes an general task in another thread using theSYSTEM_EXECUTOR.voidExecutes an general task in another thread.voidExecutes an general task in another thread using theSYSTEM_EXECUTOR.voidExecutes an general task in another thread.voidexecuteForkJoin(@NotNull Runnable task) Arranges for (asynchronous) execution of the given task inFORK_JOIN_POOL.voidexecuteForkJoin(@NotNull Runnable task, String name) Arranges for (asynchronous) execution of the given task inFORK_JOIN_POOL.@NotNull List<MetricResult> @NotNull StringgetStats()booleanvoidvoidPrint thread pool stats to log.voidpurge()Tries to remove from the work queue allFuturetasks that have been cancelled usingSYSTEM_SCHEDULERandSYSTEM_EXECUTOR.Returns a new CompletableFuture that is asynchronously completed by a task running inFORK_JOIN_POOLafter it runs the given action.@NotNull ScheduledFuture<?> Submits a one-shot task that becomes enabled after the given delay to theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> Submits a one-shot task that becomes enabled after the given delay to theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> Submits a one-shot task that becomes enabled after the given delay to theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> scheduleAtFixedRate(@NotNull Runnable task, long initialDelay, long period) Schedules a general task to be executed at fixed rate using theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> scheduleAtFixedRate(@NotNull Runnable task, long initialDelay, long period, TimeUnit unit) Schedules a general task to be executed at fixed rate using theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> scheduleAtFixedRate(@NotNull Runnable task, long initialDelay, long period, TimeUnit unit, String name) Schedules a general task to be executed at fixed rate using theSYSTEM_SCHEDULER.scheduleWithFixedDelay(@NotNull Runnable task, long initialDelay, long delay) Schedules a general task to be executed periodically with a fixed delay between the end of one execution and the start of the next using theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> scheduleWithFixedDelay(@NotNull Runnable task, long initialDelay, long delay, TimeUnit unit) Schedules a general task to be executed periodically with a fixed delay between the end of one execution and the start of the next using theSYSTEM_SCHEDULER.@NotNull ScheduledFuture<?> scheduleWithFixedDelay(@NotNull Runnable task, long initialDelay, long delay, TimeUnit unit, String name) Schedules a general task to be executed periodically with a fixed delay between the end of one execution and the start of the next using theSYSTEM_SCHEDULER.<V> @NotNull Future<V> Submits a general task to theSYSTEM_EXECUTORfor execution.<V> @NotNull Future<V> Submits a general task to the thread pool for execution.<V> @NotNull Future<V> Submits a general task to the thread pool for execution.<V> @NotNull Future<V> Submits a general task to theSYSTEM_EXECUTORfor execution.<T> @NotNull ForkJoinTask<T> submitExternalForkJoin(@NotNull ForkJoinTask<T> task, String name) Submits a ForkJoinTask to theFORK_JOIN_POOLfor execution from an external thread.voidsubmitExternalForkJoinGet(@NotNull Runnable task, String name) Submits a task to theFORK_JOIN_POOLfrom an external thread, wraps it into aRunnableWrapperand blocks until it is completed.<V> VsubmitExternalForkJoinGet(@NotNull Callable<V> task, V defaultValue, String name) Submits a task to theFORK_JOIN_POOLfrom an external thread, wraps it into aCallableWrapper, blocks until it is completed, and returns the result.ForkJoinTask<?> submitForkJoin(@NotNull Runnable task, String name) Submits a task to theFORK_JOIN_POOLfor execution wrapping it into aRunnableWrapper.<V> ForkJoinTask<V> submitForkJoin(@NotNull Callable<V> task, String name) Submits a task to theFORK_JOIN_POOLfor execution wrapping it into aCallableWrapper.<V> ForkJoinTask<V> submitForkJoin(@NotNull Callable<V> task, V defaultValue, String name) Submits a task to theFORK_JOIN_POOLfor execution wrapping it into aCallableWrapper.voidsubmitForkJoinGet(@NotNull Runnable task, String name) Submits a task to theFORK_JOIN_POOL, wraps it into aRunnableWrapperand blocks until it is completed.<V> VsubmitForkJoinGet(@NotNull Callable<V> task, V defaultValue, String name) Submits a task to theFORK_JOIN_POOL, wraps it into aCallableWrapper, blocks until it is completed, and returns the result.<U> CompletableFuture<U> supplyAsync(@NotNull Supplier<U> supplier) Returns a new CompletableFuture that is asynchronously completed by a task running inFORK_JOIN_POOLwith the value obtained by calling the given Supplier.@Nullable ThreadConvert a specified runnable task to virtual thread and start this thread usingThread.ofVirtual().voidConvert a specified runnable task to virtual thread, start this thread and waits for this thread to terminate usingtoVT(java.lang.Runnable)andThread.join(long).
-
Method Details
-
execute
Executes an general task in another thread using theSYSTEM_EXECUTOR. The task is wrapped in aRunnableWrapper.- Parameters:
task- the task to execute- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
execute
Executes an general task in another thread using theSYSTEM_EXECUTOR. The task is wrapped in aRunnableWrapper. If metrics are enabled, records the task execution.- Parameters:
task- the task to executename- optional task name for metrics- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
execute
Executes an general task in another thread. The task is wrapped in aRunnableWrapper. Uses theVIRTUAL_EXECUTORif `isVirtual` is true, otherwise uses theSYSTEM_EXECUTOR.- Parameters:
task- the task to executeisVirtual-trueif it's must be virtual thread,falseotherwise- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
execute
Executes an general task in another thread. The task is wrapped in aRunnableWrapper. Uses theVIRTUAL_EXECUTORif `isVirtual` is true, otherwise uses theSYSTEM_EXECUTOR. If metrics are enabled, records the task execution.- Parameters:
task- the task to executeisVirtual-trueif it's must be virtual thread,falseotherwisename- optional task name for metrics- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submit
Submits a general task to theSYSTEM_EXECUTORfor execution. The task is wrapped in aCallableWrapper.- Type Parameters:
V- the type of the task's result- Parameters:
c- the task to submitdefaultValue- the default value to return if the task is rejected- Returns:
- a Future representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately return the default value on get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submit
@NotNull public <V> @NotNull Future<V> submit(@NotNull @NotNull Callable<V> c, V defaultValue, String name) Submits a general task to theSYSTEM_EXECUTORfor execution. The task is wrapped in aCallableWrapper. If metrics are enabled, records the task execution.- Type Parameters:
V- the type of the task's result- Parameters:
c- the task to submitdefaultValue- the default value to return if the task is rejectedname- optional task name for metrics- Returns:
- a Future representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately return the default value on get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submit
@NotNull public <V> @NotNull Future<V> submit(@NotNull @NotNull Callable<V> c, V defaultValue, boolean virtual) Submits a general task to the thread pool for execution. The task is wrapped in aCallableWrapper. Uses theVIRTUAL_EXECUTORif `virtual` is true, otherwise uses theSYSTEM_EXECUTOR.- Type Parameters:
V- the type of the task's result- Parameters:
c- the task to submitdefaultValue- the default value to return if the task is rejectedvirtual-trueif it's must be virtual thread,falseotherwise- Returns:
- a Future representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately return the default value on get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submit
@NotNull public <V> @NotNull Future<V> submit(@NotNull @NotNull Callable<V> c, V defaultValue, boolean virtual, String name) Submits a general task to the thread pool for execution. The task is wrapped in aCallableWrapper. Uses theVIRTUAL_EXECUTORif `virtual` is true, otherwise uses theSYSTEM_EXECUTOR. If metrics are enabled, records the task execution.- Type Parameters:
V- the type of the task's result- Parameters:
c- the task to submitdefaultValue- the default value to return if the task is rejectedvirtual-trueif it's must be virtual thread,falseotherwisename- optional task name for metrics- Returns:
- a Future representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately return the default value on get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
schedule
Submits a one-shot task that becomes enabled after the given delay to theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper. UsesTimeUnit.MILLISECONDSas the default time unit if the provided unit is null.- Parameters:
task- the task to executedelay- the time from now to delay execution- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately returnnullon get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
schedule
@NotNull public @NotNull ScheduledFuture<?> schedule(@NotNull @NotNull Runnable task, long delay, TimeUnit unit) Submits a one-shot task that becomes enabled after the given delay to theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper.- Parameters:
task- the task to executedelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately returnnullon get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
schedule
@NotNull public @NotNull ScheduledFuture<?> schedule(@NotNull @NotNull Runnable task, long delay, TimeUnit unit, String name) Submits a one-shot task that becomes enabled after the given delay to theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper. If metrics are enabled, records the task execution and the scheduling delay.- Parameters:
task- the task to executedelay- the time from now to delay executionunit- the time unit of the delay parametername- optional task name for metrics- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will immediately returnnullon get(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
scheduleAtFixedRate
@NotNull public @NotNull ScheduledFuture<?> scheduleAtFixedRate(@NotNull @NotNull Runnable task, long initialDelay, long period) Schedules a general task to be executed at fixed rate using theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper. UsesTimeUnit.MILLISECONDSas the default time unit.- Parameters:
task- the task to executeinitialDelay- the initial delay in the given time unitperiod- the period between executions in the given time unit- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will always returntrueon cancel(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
scheduleAtFixedRate
@NotNull public @NotNull ScheduledFuture<?> scheduleAtFixedRate(@NotNull @NotNull Runnable task, long initialDelay, long period, TimeUnit unit) Schedules a general task to be executed at fixed rate using theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper.- Parameters:
task- the task to executeinitialDelay- the initial delay in the given time unitperiod- the period between executions in the given time unitunit- the time unit of the initialDelay and period parameters- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will always returntrueon cancel(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
scheduleAtFixedRate
@NotNull public @NotNull ScheduledFuture<?> scheduleAtFixedRate(@NotNull @NotNull Runnable task, long initialDelay, long period, TimeUnit unit, String name) Schedules a general task to be executed at fixed rate using theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper. If metrics are enabled, records the task execution and the fixed rate period.- Parameters:
task- the task to executeinitialDelay- the initial delay in the given time unitperiod- the period between executions in the given time unitunit- the time unit of the initialDelay and period parametersname- optional task name for metrics- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will always returntrueon cancel(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(@NotNull @NotNull Runnable task, long initialDelay, long delay) Schedules a general task to be executed periodically with a fixed delay between the end of one execution and the start of the next using theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper. UsesTimeUnit.MILLISECONDSas the default time unit.- Parameters:
task- the task to executeinitialDelay- the initial delay in milliseconds before the first executiondelay- the delay in milliseconds between the end of one execution and the start of the next- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will always returntrueon cancel(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
scheduleWithFixedDelay
@NotNull public @NotNull ScheduledFuture<?> scheduleWithFixedDelay(@NotNull @NotNull Runnable task, long initialDelay, long delay, TimeUnit unit) Schedules a general task to be executed periodically with a fixed delay between the end of one execution and the start of the next using theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper.- Parameters:
task- the task to executeinitialDelay- the initial delay before the first executiondelay- the delay between the end of one execution and the start of the nextunit- the time unit of the initial and delay parameters- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will always returntrueon cancel(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
scheduleWithFixedDelay
@NotNull public @NotNull ScheduledFuture<?> scheduleWithFixedDelay(@NotNull @NotNull Runnable task, long initialDelay, long delay, TimeUnit unit, String name) Schedules a general task to be executed periodically with a fixed delay between the end of one execution and the start of the next using theSYSTEM_SCHEDULER. The task is wrapped in aRunnableWrapper. If metrics are enabled, records the task execution and the fixed delay period.- Parameters:
task- the task to executeinitialDelay- the initial delay before the first executiondelay- the delay between the end of one execution and the start of the nextunit- the time unit of the initial and delay parametersname- optional task name for metrics- Returns:
- a ScheduledFuture representing pending completion of the task.
If the task is rejected, returns a
ThreadPoolService.RejectedFuturethat will always returntrueon cancel(). - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
supplyAsync
Returns a new CompletableFuture that is asynchronously completed by a task running inFORK_JOIN_POOLwith the value obtained by calling the given Supplier.- Type Parameters:
U- the function's return type- Parameters:
supplier- a function returning the value to be used to complete the returned CompletableFuture- Returns:
- the new CompletableFuture
-
runAsync
Returns a new CompletableFuture that is asynchronously completed by a task running inFORK_JOIN_POOLafter it runs the given action.- Parameters:
task- the action to run before completing the returned CompletableFuture- Returns:
- the new CompletableFuture
-
executeForkJoin
Arranges for (asynchronous) execution of the given task inFORK_JOIN_POOL. The task is wrapped in aRunnableWrapper.- Parameters:
task- the task- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
executeForkJoin
Arranges for (asynchronous) execution of the given task inFORK_JOIN_POOL. The task is wrapped in aRunnableWrapper. If metrics are enabled, records the task execution.- Parameters:
task- the taskname- optional task name for metrics- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submitForkJoinGet
Submits a task to theFORK_JOIN_POOL, wraps it into aRunnableWrapperand blocks until it is completed. If metrics are enabled, records the task execution.- Parameters:
task- the task to submitname- optional task name for metrics- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submitForkJoin
Submits a task to theFORK_JOIN_POOLfor execution wrapping it into aRunnableWrapper. If metrics are enabled, records the task execution.- Parameters:
task- the task to submitname- optional task name for metrics- Returns:
- a ForkJoinTask representing pending completion of the task
-
submitForkJoinGet
Submits a task to theFORK_JOIN_POOL, wraps it into aCallableWrapper, blocks until it is completed, and returns the result. If metrics are enabled, records the task execution.- Type Parameters:
V- the type of the task's result- Parameters:
task- the task to submitdefaultValue- the default value to return if the task failsname- optional task name for metrics- Returns:
- the result of the task, or the default value if the task fails
- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submitForkJoin
Submits a task to theFORK_JOIN_POOLfor execution wrapping it into aCallableWrapper. If metrics are enabled, records the task execution.- Type Parameters:
V- the type of the task's result- Parameters:
task- the task to submitname- optional task name for metrics- Returns:
- a ForkJoinTask representing pending completion of the task
-
submitForkJoin
public <V> ForkJoinTask<V> submitForkJoin(@NotNull @NotNull Callable<V> task, V defaultValue, String name) Submits a task to theFORK_JOIN_POOLfor execution wrapping it into aCallableWrapper. If metrics are enabled, records the task execution.- Type Parameters:
V- the type of the task's result- Parameters:
task- the task to submitdefaultValue- the default value to return if the task failsname- optional task name for metrics- Returns:
- a ForkJoinTask representing pending completion of the task
-
submitExternalForkJoinGet
public <V> V submitExternalForkJoinGet(@NotNull @NotNull Callable<V> task, V defaultValue, String name) Submits a task to theFORK_JOIN_POOLfrom an external thread, wraps it into aCallableWrapper, blocks until it is completed, and returns the result. If metrics are enabled, records the task execution.- Type Parameters:
V- the type of the task's result- Parameters:
task- the task to submitdefaultValue- the default value to return if the task failsname- optional task name for metrics- Returns:
- the result of the task, or the default value if the task fails
- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submitExternalForkJoinGet
Submits a task to theFORK_JOIN_POOLfrom an external thread, wraps it into aRunnableWrapperand blocks until it is completed. If metrics are enabled, records the task execution.- Parameters:
task- the task to submitname- optional task name for metrics- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
submitExternalForkJoin
@NotNull public <T> @NotNull ForkJoinTask<T> submitExternalForkJoin(@NotNull @NotNull ForkJoinTask<T> task, String name) Submits a ForkJoinTask to theFORK_JOIN_POOLfor execution from an external thread. If metrics are enabled, records the task execution.- Type Parameters:
T- the type of the task's result- Parameters:
task- the ForkJoinTask to submitname- optional task name for metrics- Returns:
- the submitted ForkJoinTask
-
toVT
Convert a specified runnable task to virtual thread and start this thread usingThread.ofVirtual(). If metrics are enabled, records the task execution.- Parameters:
task- task to convert- Returns:
- virtual thread or
nullif failed to create virtual thread - Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
toVTJoin
Convert a specified runnable task to virtual thread, start this thread and waits for this thread to terminate usingtoVT(java.lang.Runnable)andThread.join(long).- Parameters:
task- task to convert
-
getScheduler
- Returns:
- thread scheduler
SYSTEM_SCHEDULER
-
getExecutor
- Returns:
- thread executor
SYSTEM_EXECUTOR
-
getForkJoinPool
- Returns:
- ForkJoinPool
FORK_JOIN_POOL
-
getStats
- Returns:
- all threads executor's stats
-
printStats
public void printStats()Print thread pool stats to log. -
getMetric
-
purge
public void purge()Tries to remove from the work queue allFuturetasks that have been cancelled usingSYSTEM_SCHEDULERandSYSTEM_EXECUTOR. This method can be useful as a storage reclamation operation, that has no other impact on functionality. Cancelled tasks are never executed, but may accumulate in work queues until worker threads can actively remove them. Invoking this method instead tries to remove them now. However, this method may fail to remove tasks in the presence of interference by other threads. -
isShutdown
public boolean isShutdown()- Returns:
trueif thread pool's in shutdown mode,falseotherwise
-
onShutdown
public void onShutdown()- Specified by:
onShutdownin interfacehost.anzo.commons.interfaces.startup.IShutdownable
-