Package com.google.cloud.spanner
Class ThreadFactoryUtil
java.lang.Object
com.google.cloud.spanner.ThreadFactoryUtil
Utility class for creating a thread factory for daemon or virtual threads.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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.static ThreadFactory
tryCreateVirtualThreadFactory
(String baseNameFormat) Tries to create aThreadFactory
that creates virtual threads.static ExecutorService
tryCreateVirtualThreadPerTaskExecutor
(String baseNameFormat) Tries to create anExecutorService
that creates a new virtual thread for each task that it runs.
-
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 formattryVirtualThreads
- 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 aThreadFactory
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 anExecutorService
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.
-