Class ThreadFactoryUtil (6.83.0)

public class ThreadFactoryUtil

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

Inheritance

java.lang.Object > ThreadFactoryUtil

Static Methods

createVirtualOrPlatformDaemonThreadFactory(String baseNameFormat, boolean tryVirtualThreads)

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
Name Description
baseNameFormat String

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

tryVirtualThreads boolean

whether to try to use virtual threads if available or not

Returns
Type Description
ThreadFactory

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

tryCreateVirtualThreadFactory(String baseNameFormat)

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.

Parameter
Name Description
baseNameFormat String
Returns
Type Description
ThreadFactory

tryCreateVirtualThreadPerTaskExecutor(String baseNameFormat)

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.

Parameter
Name Description
baseNameFormat String
Returns
Type Description
ExecutorService

Constructors

ThreadFactoryUtil()

public ThreadFactoryUtil()