Class Threads

java.lang.Object
one.tranic.t.util.Threads

public class Threads 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

    • Threads

      public Threads()
  • 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.
    • 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.