Package org.nanonative.nano.core
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 theNanoThreadsimplementation, used for method chaining.
- Direct Known Subclasses:
NanoServices
The abstract base class for
Nano framework providing thread handling functionalities.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Set<ScheduledExecutorService> protected final ExecutorServiceFields inherited from class org.nanonative.nano.core.NanoBase
CONFIG_KEYS, context, createdAtMs, EVENT_ID_COUNTER, EVENT_TYPES, eventCount, isReady, listeners, logger -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNanoThreads(Map<Object, Object> config, String... args) InitializesNanoThreadswith configurations and command-line arguments. -
Method Summary
Modifier and TypeMethodDescriptionprotected SchedulerasyncFromPool(Supplier<Context> context) Creates aSchedulerfrom the thread pool.protected voidexecuteScheduler(Supplier<Context> context, ExRunnable task, Scheduler scheduler, boolean periodically) protected voidkill(ExecutorService executorService, long timeoutMs) Forces shutdown of anExecutorServiceif it doesn't terminate within the specified timeout.protected voidremoveScheduler(ExecutorService executorService) Removes a scheduler from the set of managed schedulers.run(Supplier<Context> context, ExRunnable task, long delay, long period, TimeUnit unit, BooleanSupplier until) Executes a task periodically, starting after an initial delay.run(Supplier<Context> context, ExRunnable task, long delay, TimeUnit timeUnit) Executes a task asynchronously after a specified delay.run(Supplier<Context> context, ExRunnable task, LocalTime atTime, BooleanSupplier until) Executes a task periodically, starting after an initial delay.Provides an unmodifiable set ofScheduledExecutorService.protected voidshutdownExecutors(long timeoutMs, ExecutorService... executorServices) Shuts down executors and handles timeout for forced termination.protected voidShuts down all threads and scheduled executors gracefully.Gets the thread pool executor used byNano.Methods inherited from class org.nanonative.nano.core.NanoBase
createdAtMs, displayHelpMenu, eventCount, heapMemoryUsage, isReady, listeners, logger, pid, putAll, readConfigs, setLogLevel, standardiseKey, stop, stop, subscribeEvent, unsubscribeEvent, usedMemoryMB
-
Field Details
-
schedulers
-
threadPool
-
-
Constructor Details
-
NanoThreads
InitializesNanoThreadswith configurations and command-line arguments.- Parameters:
config- Configuration parameters for theNanoThreadsinstance.args- Command-line arguments passed during the application start.
-
-
Method Details
-
threadPool
Gets the thread pool executor used byNano.- Returns:
- The ThreadPoolExecutor instance.
-
schedulers
Provides an unmodifiable set ofScheduledExecutorService.- Returns:
- An unmodifiable set of
ScheduledExecutorServiceinstances.
-
run
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.truestops the next execution.- Returns:
- Self for chaining
-
run
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.truestops the next execution.- Returns:
- Self for chaining
-
asyncFromPool
Creates aSchedulerfrom the thread pool.- Returns:
- The newly created
Scheduler.
-
shutdownThreads
protected void shutdownThreads()Shuts down all threads and scheduled executors gracefully. -
shutdownExecutors
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
Removes a scheduler from the set of managed schedulers.- Parameters:
executorService- TheExecutorServiceto remove from the scheduler set.
-
kill
Forces shutdown of anExecutorServiceif 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)
-