Package one.tranic.t.util
Class Threads
java.lang.Object
one.tranic.t.util.Threads
Provides utility methods for working with threads, including creating
virtual threads, thread factories, and executors.
This class supports fallback mechanisms for environments where virtual threads are not available, allowing compatibility across different JVM versions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorServiceProvides anExecutorServicewith specific configuration by default.static ExecutorServicegetExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates and returns anExecutorServiceinstance.static ExecutorServicegetPureExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates and returns anExecutorServiceusing the specified parameters.static @NotNull ThreadFactoryCreates a new virtual thread factory if supported by the current JVM; otherwise, falls back to the default thread factory implementation.static ExecutorServiceCreates a newExecutorServicethat assigns a virtual thread to each submitted task.static ExecutorServicenewVirtualThreadPerTaskExecutorOrProvided(ExecutorService fallbackExecutor) Creates a new virtual thread-per-task executor if supported by the runtime environment, otherwise returns the provided fallback executor.
-
Constructor Details
-
Threads
public Threads()
-
-
Method Details
-
newVirtualThreadPerTaskExecutor
Creates a newExecutorServicethat assigns a virtual thread to each submitted task. This method provides an executor service designed for lightweight concurrency tasks using virtual threads.- Returns:
- a new
ExecutorServiceconfigured to create a virtual thread for every task, ornullif virtual threads are not supported on the current JVM.
-
newVirtualThreadFactoryOrDefault
Creates a new virtual thread factory if supported by the current JVM; otherwise, falls back to the default thread factory implementation.- Returns:
- a
ThreadFactoryinstance that creates virtual threads if supported, or a default thread factory if virtual threads are not available.
-
newVirtualThreadPerTaskExecutorOrProvided
public static ExecutorService newVirtualThreadPerTaskExecutorOrProvided(ExecutorService fallbackExecutor) Creates a new virtual thread-per-task executor if supported by the runtime environment, otherwise returns the provided fallback executor.- Parameters:
fallbackExecutor- the executor service to fallback to if virtual threads are not supported- Returns:
- a virtual thread-per-task executor if available, otherwise the provided fallback executor
-
getExecutor
Provides anExecutorServicewith specific configuration by default. It utilizes a factory method to create an executor with a thread pool having an unlimited queue size but bounded by the given core and maximum pool size for concurrency management.- Returns:
- an
ExecutorServicepre-configured with the default settings of corePoolSize as 0, maximumPoolSize as 4, a keep-alive time of 15 seconds, and aSynchronousQueueas the task queue.
-
getExecutor
public static ExecutorService getExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates and returns anExecutorServiceinstance. If virtual thread support is available and properly configured, a virtual thread-based executor service will be created. Otherwise, a traditionalThreadPoolExecutoris used.- Parameters:
corePoolSize- the number of core threads to keep in the pool, even if they are idle.maximumPoolSize- the maximum number of threads allowed in the pool.keepAliveTime- the maximum time that excess idle threads will wait for new tasks before terminating.unit- the time unit for the keepAliveTime argument.workQueue- the queue to use for holding tasks before they are executed.- Returns:
- an
ExecutorServiceinstance based on the specified configurations or a virtual thread-based executor if supported.
-
getPureExecutor
public static ExecutorService getPureExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates and returns anExecutorServiceusing the specified parameters.This executor operates as a thread pool with configurations provided by the caller, utilizing a custom thread factory for virtual threads if supported.
- Parameters:
corePoolSize- the number of core threads to keep in the pool, even if they are idle.maximumPoolSize- the maximum number of threads allowed in the pool.keepAliveTime- the maximum time that excess idle threads will wait for new tasks before terminating.unit- the time unit for the keepAliveTime argument.workQueue- the queue to use for holding tasks before they are executed.- Returns:
- an
ExecutorServiceinstance configured according to the specified parameters.
-