Class T2hread

java.lang.Object
one.tranic.t.thread.T2hread

public class T2hread extends Object
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 Details

    • T2hread

      public T2hread()
  • Method Details

    • newVirtualThreadPerTaskExecutor

      public static ExecutorService newVirtualThreadPerTaskExecutor()
      Creates a new ExecutorService that 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 ExecutorService configured to create a virtual thread for every task, or null if virtual threads are not supported on the current JVM.
    • newVirtualThreadFactoryOrDefault

      @NotNull public static @NotNull ThreadFactory newVirtualThreadFactoryOrDefault()
      Creates a new virtual thread factory if supported by the current JVM; otherwise, falls back to the default thread factory implementation.
      Returns:
      a ThreadFactory instance that creates virtual threads if supported, or a default thread factory if virtual threads are not available.
    • newThread

      @NotNull public static @NotNull Thread newThread(Runnable runnable)
      Creates a new thread to execute the provided Runnable task. 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.
      Parameters:
      runnable - the Runnable task to be executed by the new thread.
      Returns:
      a Thread configured to execute the provided task, not null.
    • newThread

      @NotNull public static @NotNull Thread newThread(Runnable runnable, String name)
      Creates a new thread using the provided Runnable and 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.
      Parameters:
      runnable - the Runnable task to be executed by the thread
      name - the name to assign to the created thread
      Returns:
      the created Thread instance
    • newThread

      @NotNull public static @NotNull Thread newThread(Runnable runnable, String name, boolean daemon)
      Creates a new thread with the given runnable, name, and daemon status.
      Parameters:
      runnable - the Runnable to 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 Thread instance 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

      public static ExecutorService getExecutor()
      Provides an ExecutorService with 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 ExecutorService pre-configured with the default settings of corePoolSize as 0, maximumPoolSize as 4, a keep-alive time of 15 seconds, and a SynchronousQueue as the task queue.
    • getExecutor

      public static ExecutorService getExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
      Creates and returns an ExecutorService instance. If virtual thread support is available and properly configured, a virtual thread-based executor service will be created. Otherwise, a traditional ThreadPoolExecutor is 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 ExecutorService instance 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 an ExecutorService using 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 ExecutorService instance configured according to the specified parameters.