Class ThreadFactoryUtil

java.lang.Object
com.google.cloud.spanner.ThreadFactoryUtil

@InternalApi public class ThreadFactoryUtil extends Object
Utility class for creating a thread factory for daemon or virtual threads.
  • Constructor Details

    • ThreadFactoryUtil

      public ThreadFactoryUtil()
  • Method Details

    • createVirtualOrPlatformDaemonThreadFactory

      @InternalApi public static ThreadFactory createVirtualOrPlatformDaemonThreadFactory(String baseNameFormat, boolean tryVirtualThreads)
      Tries to create a thread factory for virtual threads, and otherwise falls back to creating a platform thread factory that creates daemon threads. Virtual threads are supported from JDK21.
      Parameters:
      baseNameFormat - the base name format for the threads, '-%d' will be appended to the actual thread name format
      tryVirtualThreads - whether to try to use virtual threads if available or not
      Returns:
      a ThreadFactory that produces virtual threads (Java 21 or higher) or platform daemon threads
    • tryCreateVirtualThreadFactory

      @InternalApi @Nullable public static ThreadFactory tryCreateVirtualThreadFactory(String baseNameFormat)
      Tries to create a ThreadFactory that creates virtual threads. Returns null if virtual threads are not supported on this JVM.
    • tryCreateVirtualThreadPerTaskExecutor

      @InternalApi @Nullable public static ExecutorService tryCreateVirtualThreadPerTaskExecutor(String baseNameFormat)
      Tries to create an ExecutorService that creates a new virtual thread for each task that it runs. Creating a new virtual thread is the recommended way to create executors using virtual threads, instead of creating a pool of virtual threads. Returns null if virtual threads are not supported on this JVM.