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 Details

    • execute

      public void execute(@NotNull @NotNull Runnable task)
      Executes an general task in another thread using the SYSTEM_EXECUTOR. The task is wrapped in a RunnableWrapper.
      Parameters:
      task - the task to execute
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • execute

      public void execute(@NotNull @NotNull Runnable task, String name)
      Executes an general task in another thread using the SYSTEM_EXECUTOR. The task is wrapped in a RunnableWrapper. If metrics are enabled, records the task execution.
      Parameters:
      task - the task to execute
      name - optional task name for metrics
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • execute

      public void execute(Runnable task, boolean isVirtual)
      Executes an general task in another thread. The task is wrapped in a RunnableWrapper. Uses the VIRTUAL_EXECUTOR if `isVirtual` is true, otherwise uses the SYSTEM_EXECUTOR.
      Parameters:
      task - the task to execute
      isVirtual - true if it's must be virtual thread, false otherwise
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • execute

      public void execute(@NotNull @NotNull Runnable task, boolean isVirtual, String name)
      Executes an general task in another thread. The task is wrapped in a RunnableWrapper. Uses the VIRTUAL_EXECUTOR if `isVirtual` is true, otherwise uses the SYSTEM_EXECUTOR. If metrics are enabled, records the task execution.
      Parameters:
      task - the task to execute
      isVirtual - true if it's must be virtual thread, false otherwise
      name - optional task name for metrics
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • submit

      @NotNull public @NotNull Future<?> submit(@NotNull @NotNull Runnable r)
      Submits a general task to the SYSTEM_EXECUTOR for execution. The task is wrapped in a RunnableWrapper.
      Parameters:
      r - the task to submit
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return the default value on get().
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • submit

      @NotNull public @NotNull Future<?> submit(@NotNull @NotNull Runnable r, String name)
      Submits a general task to the SYSTEM_EXECUTOR for execution. The task is wrapped in a RunnableWrapper. If metrics are enabled, records the task execution.
      Parameters:
      r - the task to submit
      name - optional task name for metrics
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return the default value on get().
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • submit

      @NotNull public @NotNull Future<?> submit(@NotNull @NotNull Runnable r, boolean virtual)
      Submits a general task to the thread pool for execution. The task is wrapped in a RunnableWrapper. Uses the VIRTUAL_EXECUTOR if `virtual` is true, otherwise uses the SYSTEM_EXECUTOR.
      Parameters:
      r - the task to submit
      virtual - true if it's must be virtual thread, false otherwise
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return the default value on get().
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • submit

      @NotNull public @NotNull Future<?> submit(@NotNull @NotNull Runnable r, boolean virtual, String name)
      Submits a general task to the thread pool for execution. The task is wrapped in a RunnableWrapper. Uses the VIRTUAL_EXECUTOR if `virtual` is true, otherwise uses the SYSTEM_EXECUTOR. If metrics are enabled, records the task execution.
      Parameters:
      r - the task to submit
      virtual - true if it's must be virtual thread, false otherwise
      name - optional task name for metrics
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that 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)
      Submits a general task to the SYSTEM_EXECUTOR for execution. The task is wrapped in a CallableWrapper.
      Type Parameters:
      V - the type of the task's result
      Parameters:
      c - the task to submit
      defaultValue - 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.RejectedFuture that 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 the SYSTEM_EXECUTOR for execution. The task is wrapped in a CallableWrapper. If metrics are enabled, records the task execution.
      Type Parameters:
      V - the type of the task's result
      Parameters:
      c - the task to submit
      defaultValue - the default value to return if the task is rejected
      name - optional task name for metrics
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that 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 a CallableWrapper. Uses the VIRTUAL_EXECUTOR if `virtual` is true, otherwise uses the SYSTEM_EXECUTOR.
      Type Parameters:
      V - the type of the task's result
      Parameters:
      c - the task to submit
      defaultValue - the default value to return if the task is rejected
      virtual - true if it's must be virtual thread, false otherwise
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that 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 a CallableWrapper. Uses the VIRTUAL_EXECUTOR if `virtual` is true, otherwise uses the SYSTEM_EXECUTOR. If metrics are enabled, records the task execution.
      Type Parameters:
      V - the type of the task's result
      Parameters:
      c - the task to submit
      defaultValue - the default value to return if the task is rejected
      virtual - true if it's must be virtual thread, false otherwise
      name - optional task name for metrics
      Returns:
      a Future representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return the default value on get().
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • schedule

      @NotNull public @NotNull ScheduledFuture<?> schedule(@NotNull @NotNull Runnable task, long delay)
      Submits a one-shot task that becomes enabled after the given delay to the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper. Uses TimeUnit.MILLISECONDS as the default time unit if the provided unit is null.
      Parameters:
      task - the task to execute
      delay - the time from now to delay execution
      Returns:
      a ScheduledFuture representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return null on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper.
      Parameters:
      task - the task to execute
      delay - the time from now to delay execution
      unit - the time unit of the delay parameter
      Returns:
      a ScheduledFuture representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return null on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper. If metrics are enabled, records the task execution and the scheduling delay.
      Parameters:
      task - the task to execute
      delay - the time from now to delay execution
      unit - the time unit of the delay parameter
      name - optional task name for metrics
      Returns:
      a ScheduledFuture representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will immediately return null on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper. Uses TimeUnit.MILLISECONDS as the default time unit.
      Parameters:
      task - the task to execute
      initialDelay - the initial delay in the given time unit
      period - 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.RejectedFuture that will always return true on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper.
      Parameters:
      task - the task to execute
      initialDelay - the initial delay in the given time unit
      period - the period between executions in the given time unit
      unit - 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.RejectedFuture that will always return true on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper. If metrics are enabled, records the task execution and the fixed rate period.
      Parameters:
      task - the task to execute
      initialDelay - the initial delay in the given time unit
      period - the period between executions in the given time unit
      unit - the time unit of the initialDelay and period parameters
      name - optional task name for metrics
      Returns:
      a ScheduledFuture representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will always return true on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper. Uses TimeUnit.MILLISECONDS as the default time unit.
      Parameters:
      task - the task to execute
      initialDelay - the initial delay in milliseconds before the first execution
      delay - 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.RejectedFuture that will always return true on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper.
      Parameters:
      task - the task to execute
      initialDelay - the initial delay before the first execution
      delay - the delay between the end of one execution and the start of the next
      unit - 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.RejectedFuture that will always return true on 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 the SYSTEM_SCHEDULER. The task is wrapped in a RunnableWrapper. If metrics are enabled, records the task execution and the fixed delay period.
      Parameters:
      task - the task to execute
      initialDelay - the initial delay before the first execution
      delay - the delay between the end of one execution and the start of the next
      unit - the time unit of the initial and delay parameters
      name - optional task name for metrics
      Returns:
      a ScheduledFuture representing pending completion of the task. If the task is rejected, returns a ThreadPoolService.RejectedFuture that will always return true on cancel().
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • supplyAsync

      public <U> CompletableFuture<U> supplyAsync(@NotNull @NotNull Supplier<U> supplier)
      Returns a new CompletableFuture that is asynchronously completed by a task running in FORK_JOIN_POOL 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

      public CompletableFuture<Void> runAsync(@NotNull @NotNull Runnable task)
      Returns a new CompletableFuture that is asynchronously completed by a task running in FORK_JOIN_POOL after it runs the given action.
      Parameters:
      task - the action to run before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
    • executeForkJoin

      public void executeForkJoin(@NotNull @NotNull Runnable task)
      Arranges for (asynchronous) execution of the given task in FORK_JOIN_POOL. The task is wrapped in a RunnableWrapper.
      Parameters:
      task - the task
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • executeForkJoin

      public void executeForkJoin(@NotNull @NotNull Runnable task, String name)
      Arranges for (asynchronous) execution of the given task in FORK_JOIN_POOL. The task is wrapped in a RunnableWrapper. If metrics are enabled, records the task execution.
      Parameters:
      task - the task
      name - optional task name for metrics
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • submitForkJoinGet

      public void submitForkJoinGet(@NotNull @NotNull Runnable task, String name)
      Submits a task to the FORK_JOIN_POOL, wraps it into a RunnableWrapper and blocks until it is completed. If metrics are enabled, records the task execution.
      Parameters:
      task - the task to submit
      name - optional task name for metrics
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • submitForkJoin

      public ForkJoinTask<?> submitForkJoin(@NotNull @NotNull Runnable task, String name)
      Submits a task to the FORK_JOIN_POOL for execution wrapping it into a RunnableWrapper. If metrics are enabled, records the task execution.
      Parameters:
      task - the task to submit
      name - optional task name for metrics
      Returns:
      a ForkJoinTask representing pending completion of the task
    • submitForkJoinGet

      public <V> V submitForkJoinGet(@NotNull @NotNull Callable<V> task, V defaultValue, String name)
      Submits a task to the FORK_JOIN_POOL, wraps it into a CallableWrapper, 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 submit
      defaultValue - the default value to return if the task fails
      name - 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

      public <V> ForkJoinTask<V> submitForkJoin(@NotNull @NotNull Callable<V> task, String name)
      Submits a task to the FORK_JOIN_POOL for execution wrapping it into a CallableWrapper. If metrics are enabled, records the task execution.
      Type Parameters:
      V - the type of the task's result
      Parameters:
      task - the task to submit
      name - 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 the FORK_JOIN_POOL for execution wrapping it into a CallableWrapper. If metrics are enabled, records the task execution.
      Type Parameters:
      V - the type of the task's result
      Parameters:
      task - the task to submit
      defaultValue - the default value to return if the task fails
      name - 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 the FORK_JOIN_POOL from an external thread, wraps it into a CallableWrapper, 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 submit
      defaultValue - the default value to return if the task fails
      name - 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

      public void submitExternalForkJoinGet(@NotNull @NotNull Runnable task, String name)
      Submits a task to the FORK_JOIN_POOL from an external thread, wraps it into a RunnableWrapper and blocks until it is completed. If metrics are enabled, records the task execution.
      Parameters:
      task - the task to submit
      name - 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 the FORK_JOIN_POOL for 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 submit
      name - optional task name for metrics
      Returns:
      the submitted ForkJoinTask
    • toVT

      @Nullable public @Nullable Thread toVT(@NotNull @NotNull Runnable task)
      Convert a specified runnable task to virtual thread and start this thread using Thread.ofVirtual(). If metrics are enabled, records the task execution.
      Parameters:
      task - task to convert
      Returns:
      virtual thread or null if failed to create virtual thread
      Throws:
      RejectedExecutionException - if the task cannot be accepted for execution
    • toVTJoin

      public void toVTJoin(Runnable task)
      Convert a specified runnable task to virtual thread, start this thread and waits for this thread to terminate using toVT(java.lang.Runnable) and Thread.join(long).
      Parameters:
      task - task to convert
    • getScheduler

      public ScheduledExecutorService getScheduler()
      Returns:
      thread scheduler SYSTEM_SCHEDULER
    • getExecutor

      public ExecutorService getExecutor()
      Returns:
      thread executor SYSTEM_EXECUTOR
    • getForkJoinPool

      public ForkJoinPool getForkJoinPool()
      Returns:
      ForkJoinPool FORK_JOIN_POOL
    • getStats

      @NotNull public @NotNull String getStats()
      Returns:
      all threads executor's stats
    • printStats

      public void printStats()
      Print thread pool stats to log.
    • getMetric

      @NotNull public @NotNull List<MetricResult> getMetric()
      Specified by:
      getMetric in interface IMetric
    • purge

      public void purge()
      Tries to remove from the work queue all Future tasks that have been cancelled using SYSTEM_SCHEDULER and SYSTEM_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:
      true if thread pool's in shutdown mode, false otherwise
    • onShutdown

      public void onShutdown()
      Specified by:
      onShutdown in interface host.anzo.commons.interfaces.startup.IShutdownable