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 sometime in future in another thread.voidExecutes an general task sometime in future in another thread.voidExecutes an general task sometime in future in another thread.voidExecutes an general task sometime in future in another thread.voidexecuteForkJoin(Runnable task) Arranges for (asynchronous) execution of the given task in ForkJoinPoolvoidexecuteForkJoin(Runnable task, String name) Arranges for (asynchronous) execution of the given task in ForkJoinPool@NotNull List<MetricResult> @NotNull StringgetStats()booleanvoidvoidPrint thread pool stats to log.voidpurge()Tries to remove from the work queue allFuturetasks that have been cancelled.Returns a new CompletableFuture that is asynchronously completed by a task running in ForkJoinPool after it runs the given action.Submits a one-shot task that becomes enabled after the given delay.@Nullable ScheduledFuture<?> Submits a one-shot task that becomes enabled after the given delay.@Nullable ScheduledFuture<?> Submits a one-shot task that becomes enabled after the given delay.scheduleAtFixedRate(Runnable task, long initialDelay, long period) Schedules a general task to be executed at fixed rate (in milliseconds)@Nullable ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit) Schedules a general task to be executed at fixed rate.@Nullable ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit, String name) Schedules a general task to be executed at fixed rate.Future<?> Submits a Runnable task for execution and returns a Future representing that task.@Nullable Future<?> Submits a Runnable task for execution and returns a Future representing that task.@Nullable Future<?> Submits a Runnable task for execution and returns a Future representing that task.Future<?> Submits a Runnable task for execution and returns a Future representing that task.@Nullable ForkJoinTask<?> submitForkJoin(Runnable task, String name) Submits a task to ForkJoin pool for execution.voidsubmitForkJoinGet(Runnable task) Submits a specified task to ForkJoin pool for execution and wait until a task completedvoidsubmitForkJoinGet(Runnable task, String name) Submits a specified task to ForkJoin pool for execution and wait until a task completed<U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) Returns a new CompletableFuture that is asynchronously completed by a task running in ForkJoinPool with the value obtained by calling the given Supplier.@Nullable ThreadConvert a specified runnable task to virtual thread and start this threadvoidConvert a specified runnable task to virtual thread, start this thread and waits for this thread to terminate.
-
Method Details
-
execute
Executes an general task sometime in future in another thread.- Parameters:
task- the task to execute
-
execute
Executes an general task sometime in future in another thread.- Parameters:
task- the task to executename- optional task name for metrics
-
execute
Executes an general task sometime in future in another thread.- Parameters:
task- the task to executeisVirtual-trueif it's must be virtual thread,falseotherwise
-
execute
Executes an general task sometime in future in another thread.- Parameters:
task- the task to executeisVirtual-trueif it's must be virtual thread,falseotherwisename- optional task name for metrics
-
submit
Submits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will returnnullupon successful completion.- Parameters:
task- the task to submit- Returns:
- a Future representing pending completion of the task
-
submit
Submits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will returnnullupon successful completion.- Parameters:
task- the task to submitname- optional task name for metrics- Returns:
- a Future representing pending completion of the task
-
submit
Submits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will returnnullupon successful completion.- Parameters:
task- the task to submitisVirtual-trueif it's must be virtual thread,falseotherwise- Returns:
- a Future representing pending completion of the task
-
submit
Submits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will returnnullupon successful completion.- Parameters:
task- the task to submitisVirtual-trueif it's must be virtual thread,falseotherwisename- optional task name for metrics- Returns:
- a Future representing pending completion of the task
-
schedule
Submits a one-shot task that becomes enabled after the given delay.- Parameters:
task- the task to executedelay- the time from now to delay execution- Returns:
- a ScheduledFuture representing pending completion of
the task and whose
get()method will returnnullupon completion
-
schedule
Submits a one-shot task that becomes enabled after the given delay.- 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 and whose
get()method will returnnullupon completion
-
schedule
@Nullable public @Nullable ScheduledFuture<?> schedule(Runnable task, long delay, TimeUnit unit, String name) Submits a one-shot task that becomes enabled after the given 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 and whose
get()method will returnnullupon completion
-
scheduleAtFixedRate
Schedules a general task to be executed at fixed rate (in milliseconds)- 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, and whose get() method will throw an exception upon cancellation
-
scheduleAtFixedRate
@Nullable public @Nullable ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit) Schedules a general task to be executed at fixed rate.- 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, and whose get() method will throw an exception upon cancellation
-
scheduleAtFixedRate
@Nullable public @Nullable ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit, String name) Schedules a general task to be executed at fixed rate.- 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, and whose get() method will throw an exception upon cancellation
-
supplyAsync
Returns a new CompletableFuture that is asynchronously completed by a task running in ForkJoinPool with 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 in ForkJoinPool after 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 in ForkJoinPool- Parameters:
task- the task
-
executeForkJoin
Arranges for (asynchronous) execution of the given task in ForkJoinPool- Parameters:
task- the taskname- optional task name for metrics
-
submitForkJoin
Submits a task to ForkJoin pool for execution.- Parameters:
task- the task to submitname- optional task name for metrics- Returns:
- the task
-
submitForkJoinGet
Submits a specified task to ForkJoin pool for execution and wait until a task completed- Parameters:
task- the task to submit
-
submitForkJoinGet
Submits a specified task to ForkJoin pool for execution and wait until a task completed- Parameters:
task- the task to submitname- optional task name for metrics
-
toVT
Convert a specified runnable task to virtual thread and start this thread- Parameters:
task- task to convert- Returns:
- virtual thread or
nullif failed to create virtual thread
-
toVTJoin
Convert a specified runnable task to virtual thread, start this thread and waits for this thread to terminate.- Parameters:
task- task to convert
-
getScheduler
- Returns:
- thread scheduler
-
getExecutor
- Returns:
- thread executor
-
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. 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
-