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(Runnable task)
      Executes an general task sometime in future in another thread.
      Parameters:
      task - the task to execute
    • execute

      public void execute(Runnable task, String name)
      Executes an general task sometime in future in another thread.
      Parameters:
      task - the task to execute
      name - optional task name for metrics
    • execute

      public void execute(Runnable task, boolean isVirtual)
      Executes an general task sometime in future in another thread.
      Parameters:
      task - the task to execute
      isVirtual - true if it's must be virtual thread, false otherwise
    • execute

      public void execute(Runnable task, boolean isVirtual, String name)
      Executes an general task sometime in future in another thread.
      Parameters:
      task - the task to execute
      isVirtual - true if it's must be virtual thread, false otherwise
      name - optional task name for metrics
    • submit

      public Future<?> submit(Runnable task)
      Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.
      Parameters:
      task - the task to submit
      Returns:
      a Future representing pending completion of the task
    • submit

      public Future<?> submit(Runnable task, String name)
      Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.
      Parameters:
      task - the task to submit
      name - optional task name for metrics
      Returns:
      a Future representing pending completion of the task
    • submit

      @Nullable public @Nullable Future<?> submit(Runnable task, boolean isVirtual)
      Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.
      Parameters:
      task - the task to submit
      isVirtual - true if it's must be virtual thread, false otherwise
      Returns:
      a Future representing pending completion of the task
    • submit

      @Nullable public @Nullable Future<?> submit(Runnable task, boolean isVirtual, String name)
      Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.
      Parameters:
      task - the task to submit
      isVirtual - 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
    • schedule

      public ScheduledFuture<?> schedule(Runnable task, long delay)
      Submits a one-shot task that becomes enabled after the given delay.
      Parameters:
      task - the task to execute
      delay - the time from now to delay execution
      Returns:
      a ScheduledFuture representing pending completion of the task and whose get() method will return null upon completion
    • schedule

      @Nullable public @Nullable ScheduledFuture<?> schedule(Runnable task, long delay, TimeUnit unit)
      Submits a one-shot task that becomes enabled after the given delay.
      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 and whose get() method will return null upon 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 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 and whose get() method will return null upon completion
    • scheduleAtFixedRate

      public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period)
      Schedules a general task to be executed at fixed rate (in milliseconds)
      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, 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 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, 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 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, and whose get() method will throw an exception upon cancellation
    • supplyAsync

      public <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.
      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(Runnable task)
      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

      public void executeForkJoin(Runnable task)
      Arranges for (asynchronous) execution of the given task in ForkJoinPool
      Parameters:
      task - the task
    • executeForkJoin

      public void executeForkJoin(Runnable task, String name)
      Arranges for (asynchronous) execution of the given task in ForkJoinPool
      Parameters:
      task - the task
      name - optional task name for metrics
    • submitForkJoin

      @Nullable public @Nullable ForkJoinTask<?> submitForkJoin(Runnable task, String name)
      Submits a task to ForkJoin pool for execution.
      Parameters:
      task - the task to submit
      name - optional task name for metrics
      Returns:
      the task
    • submitForkJoinGet

      public void submitForkJoinGet(Runnable task)
      Submits a specified task to ForkJoin pool for execution and wait until a task completed
      Parameters:
      task - the task to submit
    • submitForkJoinGet

      public void submitForkJoinGet(Runnable task, String name)
      Submits a specified task to ForkJoin pool for execution and wait until a task completed
      Parameters:
      task - the task to submit
      name - optional task name for metrics
    • toVT

      @Nullable public @Nullable Thread toVT(Runnable task)
      Convert a specified runnable task to virtual thread and start this thread
      Parameters:
      task - task to convert
      Returns:
      virtual thread or null if failed to create virtual thread
    • toVTJoin

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

      public ScheduledExecutorService getScheduler()
      Returns:
      thread scheduler
    • getExecutor

      public ExecutorService getExecutor()
      Returns:
      thread executor
    • 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. 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