Class ThreadFactoryUtil (6.89.0)

publicclassThreadFactoryUtil

Utility class for creating a thread factory for daemon or virtual threads.

Inheritance

java.lang.Object > ThreadFactoryUtil

Static Methods

createVirtualOrPlatformDaemonThreadFactory(String baseNameFormat, boolean tryVirtualThreads)

publicstaticThreadFactorycreateVirtualOrPlatformDaemonThreadFactory(StringbaseNameFormat,booleantryVirtualThreads)

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
NameDescription
baseNameFormatString

the base name format for the threads, '-%d' will be appended to the actual thread name format

tryVirtualThreadsboolean

whether to try to use virtual threads if available or not

Returns
TypeDescription
ThreadFactory

a ThreadFactory that produces virtual threads (Java 21 or higher) or platform daemon threads

tryCreateVirtualThreadFactory(String baseNameFormat)

publicstaticThreadFactorytryCreateVirtualThreadFactory(StringbaseNameFormat)

Tries to create a ThreadFactory that creates virtual threads. Returns null if virtual threads are not supported on this JVM.

Parameter
NameDescription
baseNameFormatString
Returns
TypeDescription
ThreadFactory

tryCreateVirtualThreadPerTaskExecutor(String baseNameFormat)

publicstaticExecutorServicetryCreateVirtualThreadPerTaskExecutor(StringbaseNameFormat)

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.

Parameter
NameDescription
baseNameFormatString
Returns
TypeDescription
ExecutorService

Constructors

ThreadFactoryUtil()

publicThreadFactoryUtil()