public class ConfigurableThreadFactory
extends java.lang.Object
implements java.util.concurrent.ThreadFactory
ThreadFactory which is configurable for the most common use cases.
Specifically, it allows you several options for how to prefix the name of threads, if returned
threads should be daemon or not, what their priority should be, and if an
Thread.UncaughtExceptionHandler should be provided to new threads. You can construct this
with no arguments, and it's behavior will match that of
Executors.defaultThreadFactory().| Modifier and Type | Class and Description |
|---|---|
static class |
ConfigurableThreadFactory.ConfigurableThreadFactoryBuilder
Builder for configuring a new
ConfigurableThreadFactory. |
| Constructor and Description |
|---|
ConfigurableThreadFactory()
Constructs a new
ConfigurableThreadFactory with the default parameters. |
ConfigurableThreadFactory(boolean useDaemonThreads)
Constructs a new
ConfigurableThreadFactory specifying the behavior for if threads
should be daemon or not. |
ConfigurableThreadFactory(ExceptionHandler defaultThreadlyExceptionHandler)
Constructs a new
ConfigurableThreadFactory specifying an ExceptionHandler
that will be provided to all newly created threads. |
ConfigurableThreadFactory(int threadPriority)
Constructs a new
ConfigurableThreadFactory specifying the priority for produced
threads. |
ConfigurableThreadFactory(java.lang.String threadNamePrefix,
boolean appendPoolIdToPrefix)
Constructs a new
ConfigurableThreadFactory specifying the prefix for the name of
newly created threads. |
ConfigurableThreadFactory(java.lang.String threadNamePrefix,
boolean appendPoolIdToPrefix,
boolean useDaemonThreads,
int threadPriority,
java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
ExceptionHandler defaultThreadlyExceptionHandler)
Constructs a new
ConfigurableThreadFactory allowing you to provide specific values
for everything which this class allows to be configured. |
ConfigurableThreadFactory(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
Constructs a new
ConfigurableThreadFactory specifying an
Thread.UncaughtExceptionHandler that will be provided to all newly created threads. |
| Modifier and Type | Method and Description |
|---|---|
static ConfigurableThreadFactory.ConfigurableThreadFactoryBuilder |
builder()
Construct a new builder as an alternative to the large full parameter constructor when multiple
features are needing to be configured.
|
java.lang.Thread |
newThread(java.lang.Runnable r) |
public ConfigurableThreadFactory()
ConfigurableThreadFactory with the default parameters. Threads
produced from this should behave exactly like Executors.defaultThreadFactory(), except the
pool number provided in the thread name will be respective to the ones created from other
ConfigurableThreadFactory instances.public ConfigurableThreadFactory(java.lang.String threadNamePrefix,
boolean appendPoolIdToPrefix)
ConfigurableThreadFactory specifying the prefix for the name of
newly created threads.
If specified with true for appendPoolIdToPrefix it will append a unique
"pool" id to the prefix, giving it the format of
threadNamePrefix + UNIQUE_POOL_ID + "-thread-". If appendPoolIdToPrefix is
specified as false, only a unique thread id will be appended to the prefix. In
either case, the produced threads name will be appended with a unique thread id for the
factory instance.
threadNamePrefix - prefix for all threads createdappendPoolIdToPrefix - true to append a unique pool id to the thread prefixpublic ConfigurableThreadFactory(boolean useDaemonThreads)
ConfigurableThreadFactory specifying the behavior for if threads
should be daemon or not.useDaemonThreads - true if produced threads should be daemon threadspublic ConfigurableThreadFactory(int threadPriority)
ConfigurableThreadFactory specifying the priority for produced
threads.
If the priority is below or above the max available thread priority, this will be adjusted to the limit of the system.
threadPriority - Priority for newly created threadspublic ConfigurableThreadFactory(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
ConfigurableThreadFactory specifying an
Thread.UncaughtExceptionHandler that will be provided to all newly created threads.defaultUncaughtExceptionHandler - Thread.UncaughtExceptionHandler to provide to newly created threadspublic ConfigurableThreadFactory(ExceptionHandler defaultThreadlyExceptionHandler)
ConfigurableThreadFactory specifying an ExceptionHandler
that will be provided to all newly created threads.defaultThreadlyExceptionHandler - ExceptionHandler to provide to newly created threadspublic ConfigurableThreadFactory(java.lang.String threadNamePrefix,
boolean appendPoolIdToPrefix,
boolean useDaemonThreads,
int threadPriority,
java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
ExceptionHandler defaultThreadlyExceptionHandler)
ConfigurableThreadFactory allowing you to provide specific values
for everything which this class allows to be configured. You must use this constructor if
you need to adjust two or more values.
If specified with true for appendPoolIdToPrefix it will append a unique
"pool" id to the prefix, giving it the format of
threadNamePrefix + UNIQUE_POOL_ID + "-thread-". If appendPoolIdToPrefix is
specified as false, only a unique thread id will be appended to the prefix. In
either case, the produced threads name will be appended with a unique thread id for the
factory instance.
If the priority is below or above the max available thread priority, this will be adjusted to the limit of the system.
threadNamePrefix - prefix for all threads created, null to match defaultappendPoolIdToPrefix - true to append a unique pool id to the thread prefix,
true to match defaultuseDaemonThreads - true if produced threads should be daemon threads, false to match defaultthreadPriority - Priority for newly created threads, Thread.NORM_PRIORITY to match defaultuncaughtExceptionHandler - UncaughtExceptionHandler to provide to newly created threads,
null to match defaultdefaultThreadlyExceptionHandler - ExceptionHandler to provide to newly created threadspublic static ConfigurableThreadFactory.ConfigurableThreadFactoryBuilder builder()
Defaults (equivalent to ConfigurableThreadFactory()):
threadNamePrefix: "pool-"
appendPoolIdToPrefix: true
useDaemonThreads: false
threadPriority: Thread.NORM_PRIORITY
threadlyExceptionHandler: null
build()public java.lang.Thread newThread(java.lang.Runnable r)
newThread in interface java.util.concurrent.ThreadFactory