Class ConfigurableThreadFactory
- java.lang.Object
-
- org.threadly.concurrent.ConfigurableThreadFactory
-
- All Implemented Interfaces:
java.util.concurrent.ThreadFactory
- Direct Known Subclasses:
ThreadReferencingThreadFactory
public class ConfigurableThreadFactory extends java.lang.Object implements java.util.concurrent.ThreadFactory
Implementation ofThreadFactory
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 anThread.UncaughtExceptionHandler
should be provided to new threads. You can construct this with no arguments, and it's behavior will match that ofExecutors.defaultThreadFactory()
.- Since:
- 2.3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConfigurableThreadFactory.ConfigurableThreadFactoryBuilder
Builder for configuring a newConfigurableThreadFactory
.
-
Constructor Summary
Constructors Constructor Description ConfigurableThreadFactory()
Constructs a newConfigurableThreadFactory
with the default parameters.ConfigurableThreadFactory(boolean useDaemonThreads)
Constructs a newConfigurableThreadFactory
specifying the behavior for if threads should be daemon or not.ConfigurableThreadFactory(int threadPriority)
Constructs a newConfigurableThreadFactory
specifying the priority for produced threads.ConfigurableThreadFactory(java.lang.String threadNamePrefix, boolean appendPoolIdToPrefix)
Constructs a newConfigurableThreadFactory
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, java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
Constructs a newConfigurableThreadFactory
allowing you to provide specific values for everything which this class allows to be configured.ConfigurableThreadFactory(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
Constructs a newConfigurableThreadFactory
specifying anThread.UncaughtExceptionHandler
that will be provided to all newly created threads.ConfigurableThreadFactory(java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
Constructs a newConfigurableThreadFactory
specifying aConsumer
that will be provided threads as they created.ConfigurableThreadFactory(ExceptionHandler defaultThreadlyExceptionHandler)
Constructs a newConfigurableThreadFactory
specifying anExceptionHandler
that will be provided to all newly created threads.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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)
-
-
-
Constructor Detail
-
ConfigurableThreadFactory
public ConfigurableThreadFactory()
Constructs a newConfigurableThreadFactory
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 otherConfigurableThreadFactory
instances.
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(java.lang.String threadNamePrefix, boolean appendPoolIdToPrefix)
Constructs a newConfigurableThreadFactory
specifying the prefix for the name of newly created threads.If specified with
true
forappendPoolIdToPrefix
it will append a unique "pool" id to the prefix, giving it the format ofthreadNamePrefix + UNIQUE_POOL_ID + "-thread-"
. IfappendPoolIdToPrefix
is specified asfalse
, 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.- Parameters:
threadNamePrefix
- prefix for all threads createdappendPoolIdToPrefix
-true
to append a unique pool id to the thread prefix
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(boolean useDaemonThreads)
Constructs a newConfigurableThreadFactory
specifying the behavior for if threads should be daemon or not.- Parameters:
useDaemonThreads
-true
if produced threads should be daemon threads
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(int threadPriority)
Constructs a newConfigurableThreadFactory
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.
- Parameters:
threadPriority
- Priority for newly created threads
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
Constructs a newConfigurableThreadFactory
specifying anThread.UncaughtExceptionHandler
that will be provided to all newly created threads.- Parameters:
defaultUncaughtExceptionHandler
-Thread.UncaughtExceptionHandler
to provide to newly created threads
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(ExceptionHandler defaultThreadlyExceptionHandler)
Constructs a newConfigurableThreadFactory
specifying anExceptionHandler
that will be provided to all newly created threads.- Parameters:
defaultThreadlyExceptionHandler
-ExceptionHandler
to provide to newly created threads
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
Constructs a newConfigurableThreadFactory
specifying aConsumer
that will be provided threads as they created.- Parameters:
notifyThreadCreation
- Consumer to be provided whenever a new thread is about to be returned ornull
-
ConfigurableThreadFactory
public ConfigurableThreadFactory(java.lang.String threadNamePrefix, boolean appendPoolIdToPrefix, boolean useDaemonThreads, int threadPriority, java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler, ExceptionHandler defaultThreadlyExceptionHandler, java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
Constructs a newConfigurableThreadFactory
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
forappendPoolIdToPrefix
it will append a unique "pool" id to the prefix, giving it the format ofthreadNamePrefix + UNIQUE_POOL_ID + "-thread-"
. IfappendPoolIdToPrefix
is specified asfalse
, 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.
- Parameters:
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 threadsnotifyThreadCreation
- Consumer to be provided whenever a new thread is about to be returned ornull
-
-
Method Detail
-
builder
public 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.Defaults (equivalent to
ConfigurableThreadFactory()
):threadNamePrefix
: "pool-"appendPoolIdToPrefix
:true
useDaemonThreads
:false
threadPriority
:Thread.NORM_PRIORITY
threadlyExceptionHandler
:null
- Returns:
- A new builder which can be configured then finally constructed using
build()
- Since:
- 5.39
-
newThread
public java.lang.Thread newThread(java.lang.Runnable r)
- Specified by:
newThread
in interfacejava.util.concurrent.ThreadFactory
-
-