Package one.tranic.t.thread
Class T2hread
java.lang.Object
one.tranic.t.thread.T2hread
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 ThreadCreates a new thread to execute the providedRunnabletask.static @NotNull ThreadCreates a new thread using the providedRunnableand assigns it the specified name.static @NotNull ThreadCreates a new thread with the given runnable, name, and daemon status.static @NotNull ThreadnewThread(Runnable runnable, String name, boolean daemon, Thread.UncaughtExceptionHandler handler) Creates a new thread with the specified configuration.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
-
T2hread
public T2hread()
-
-
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.
-
newThread
Creates a new thread to execute the providedRunnabletask. If virtual threads are supported by the underlying JVM, this method will create a virtual thread using a suitable thread factory; otherwise, it will fall back to a standard thread implementation. -
newThread
Creates a new thread using the providedRunnableand assigns it the specified name. This method attempts to use a virtual thread factory if virtual threads are supported; otherwise, it falls back to a default thread factory implementation. -
newThread
Creates a new thread with the given runnable, name, and daemon status.- Parameters:
runnable- theRunnableto be executed by the new thread.name- the name of the new thread.daemon- whether the thread should be a daemon thread.- Returns:
- a new
Threadinstance configured with the specified parameters.
-
newThread
@NotNull public static @NotNull Thread newThread(Runnable runnable, String name, boolean daemon, Thread.UncaughtExceptionHandler handler) Creates a new thread with the specified configuration. This method allows setting the thread's name, daemon status, and custom uncaught exception handler.- Parameters:
runnable- the task to be executed by the new thread.name- the name of the new thread.daemon- whether the new thread should be a daemon thread.handler- the handler to be invoked when the thread terminates due to an uncaught exception.- Returns:
- a newly created thread configured with the given properties.
-
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.
-