Class NanoThreads<T extends NanoThreads<T>>

java.lang.Object
org.nanonative.nano.core.NanoBase<T>
org.nanonative.nano.core.NanoThreads<T>
Type Parameters:
T - The type of the NanoThreads implementation, used for method chaining.
Direct Known Subclasses:
NanoServices

public abstract class NanoThreads<T extends NanoThreads<T>> extends NanoBase<T>
The abstract base class for Nano framework providing thread handling functionalities.
  • Field Details

  • Constructor Details

    • NanoThreads

      protected NanoThreads(Map<Object,Object> config, String... args)
      Initializes NanoThreads with configurations and command-line arguments.
      Parameters:
      config - Configuration parameters for the NanoThreads instance.
      args - Command-line arguments passed during the application start.
  • Method Details

    • threadPool

      public ExecutorService threadPool()
      Gets the thread pool executor used by Nano.
      Returns:
      The ThreadPoolExecutor instance.
    • schedulers

      public Set<ScheduledExecutorService> schedulers()
      Provides an unmodifiable set of ScheduledExecutorService.
      Returns:
      An unmodifiable set of ScheduledExecutorService instances.
    • run

      public T run(Supplier<Context> context, ExRunnable task, long delay, TimeUnit timeUnit)
      Executes a task asynchronously after a specified delay.
      Parameters:
      task - The task to execute.
      delay - The delay before executing the task.
      timeUnit - The time unit of the delay parameter.
      Returns:
      Self for chaining
    • run

      public T run(Supplier<Context> context, ExRunnable task, long delay, long period, TimeUnit unit, BooleanSupplier until)
      Executes a task periodically, starting after an initial delay.
      Parameters:
      task - The task to execute.
      delay - The initial delay before executing the task.
      period - The period between successive task executions.
      unit - The time unit of the initialDelay and period parameters.
      until - A BooleanSupplier indicating the termination condition. true stops the next execution.
      Returns:
      Self for chaining
    • run

      public T run(Supplier<Context> context, ExRunnable task, LocalTime atTime, BooleanSupplier until)
      Executes a task periodically, starting after an initial delay.
      Parameters:
      task - The task to execute.
      atTime - The time of hour/minute/second to start the task.
      until - A BooleanSupplier indicating the termination condition. true stops the next execution.
      Returns:
      Self for chaining
    • asyncFromPool

      protected Scheduler asyncFromPool(Supplier<Context> context)
      Creates a Scheduler from the thread pool.
      Returns:
      The newly created Scheduler.
    • shutdownThreads

      protected void shutdownThreads()
      Shuts down all threads and scheduled executors gracefully.
    • shutdownExecutors

      protected void shutdownExecutors(long timeoutMs, ExecutorService... executorServices)
      Shuts down executors and handles timeout for forced termination.
      Parameters:
      timeoutMs - The maximum time to wait for executor termination.
      executorServices - An array of ExecutorService instances to shut down.
    • removeScheduler

      protected void removeScheduler(ExecutorService executorService)
      Removes a scheduler from the set of managed schedulers.
      Parameters:
      executorService - The ExecutorService to remove from the scheduler set.
    • kill

      protected void kill(ExecutorService executorService, long timeoutMs) throws InterruptedException
      Forces shutdown of an ExecutorService if it doesn't terminate within the specified timeout.
      Parameters:
      executorService - The executor service to shut down.
      timeoutMs - The maximum time to wait for termination.
      Throws:
      InterruptedException - if interrupted while waiting.
    • executeScheduler

      protected void executeScheduler(Supplier<Context> context, ExRunnable task, Scheduler scheduler, boolean periodically)