Class ThreadFactoryBuilder
- whether threads should be marked as daemon threads
- a naming format
- a thread priority
- an uncaught exception handler
- a backing thread factory
If no backing thread factory is provided, a default backing thread factory is used as if by
 calling setThreadFactory(Executors.defaultThreadFactory()).
 
Java 21+ users: consider using the Thread.Builder interface instead. E.g.,
 instead of new ThreadFactoryBuilder().setPriority(priority).setDaemon(false).build(), use
 Thread.ofPlatform().priority(priority).daemon(false).factory().
- Since:
- 4.0
- Author:
- Kurt Alfred Kluever
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionbuild()Returns a new thread factory using the options supplied during the building process.setDaemon(boolean daemon) Sets daemon or not for new threads created with this ThreadFactory.setNameFormat(String nameFormat) Sets the naming format to use when naming threads (Thread.setName(java.lang.String)) which are created with this ThreadFactory.setPriority(int priority) Sets the priority for new threads created with this ThreadFactory.setThreadFactory(ThreadFactory backingThreadFactory) Sets the backingThreadFactoryfor new threads created with this ThreadFactory.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets theThread.UncaughtExceptionHandlerfor new threads created with this ThreadFactory.
- 
Constructor Details- 
ThreadFactoryBuilderpublic ThreadFactoryBuilder()Creates a newThreadFactorybuilder.Java 21+ users: use Thread.ofPlatform()instead, translating other calls on the builder as documented on each method (except for the rarely usedsetThreadFactory(java.util.concurrent.ThreadFactory), which does not have an equivalent).
 
- 
- 
Method Details- 
setNameFormatSets the naming format to use when naming threads (Thread.setName(java.lang.String)) which are created with this ThreadFactory.Java 21+ users: use Thread.Builder.name(String, long)instead. Note thatsetNameFormat(java.lang.String)accepts a thread name format string (e.g.,threadFactoryBuilder.setNameFormat("rpc-pool-%d")), whilethreadBuilder.name()accepts a thread name prefix and initial counter value (e.g.,threadBuilder.name("rpc-pool-", 0).- Parameters:
- nameFormat- a- String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example,- "rpc-pool-%d"will generate thread names like- "rpc-pool-0",- "rpc-pool-1",- "rpc-pool-2", etc.
- Returns:
- this for the builder pattern
 
- 
setDaemonSets daemon or not for new threads created with this ThreadFactory.Java 21+ users: use Thread.Builder.OfPlatform.daemon(boolean)instead.- Parameters:
- daemon- whether or not new Threads created with this ThreadFactory will be daemon threads
- Returns:
- this for the builder pattern
 
- 
setPrioritySets the priority for new threads created with this ThreadFactory.Warning: relying on the thread scheduler is discouraged. Java 21+ users: use Thread.Builder.OfPlatform.priority(int)instead.- Parameters:
- priority- the priority for new Threads created with this ThreadFactory
- Returns:
- this for the builder pattern
 
- 
setUncaughtExceptionHandler@CanIgnoreReturnValue public ThreadFactoryBuilder setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets theThread.UncaughtExceptionHandlerfor new threads created with this ThreadFactory.Java 21+ users: use Thread.Builder.uncaughtExceptionHandler(Thread.UncaughtExceptionHandler)instead.- Parameters:
- uncaughtExceptionHandler- the uncaught exception handler for new Threads created with this ThreadFactory
- Returns:
- this for the builder pattern
 
- 
setThreadFactory@CanIgnoreReturnValue public ThreadFactoryBuilder setThreadFactory(ThreadFactory backingThreadFactory) Sets the backingThreadFactoryfor new threads created with this ThreadFactory. Threads will be created by invoking #newThread(Runnable) on this backingThreadFactory.- Parameters:
- backingThreadFactory- the backing- ThreadFactorywhich will be delegated to during thread creation.
- Returns:
- this for the builder pattern
- See Also:
 
- 
buildReturns a new thread factory using the options supplied during the building process. After building, it is still possible to change the options used to build the ThreadFactory and/or build again. State is not shared amongst built instances.Java 21+ users: use Thread.Builder.factory()instead.- Returns:
- the fully constructed ThreadFactory
 
 
-