Class ThreadReferencingThreadFactory
- java.lang.Object
-
- org.threadly.concurrent.ConfigurableThreadFactory
-
- org.threadly.concurrent.ThreadReferencingThreadFactory
-
- All Implemented Interfaces:
java.util.concurrent.ThreadFactory
public class ThreadReferencingThreadFactory extends ConfigurableThreadFactory
A thread factory which keeps aWeakReference
to each thread. These threads (if still referenced by the VM) can then be retrieved by usinggetThreads(boolean)
.- Since:
- 5.15
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ThreadReferencingThreadFactory.ThreadReferencingThreadFactoryBuilder
Builder for configuring a newThreadReferencingThreadFactory
.-
Nested classes/interfaces inherited from class org.threadly.concurrent.ConfigurableThreadFactory
ConfigurableThreadFactory.ConfigurableThreadFactoryBuilder
-
-
Constructor Summary
Constructors Constructor Description ThreadReferencingThreadFactory()
Constructs a newThreadReferencingThreadFactory
with the default parameters.ThreadReferencingThreadFactory(boolean useDaemonThreads)
Constructs a newThreadReferencingThreadFactory
specifying the behavior for if threads should be daemon or not.ThreadReferencingThreadFactory(int threadPriority)
Constructs a newThreadReferencingThreadFactory
specifying the priority for produced threads.ThreadReferencingThreadFactory(java.lang.String threadNamePrefix, boolean appendPoolIdToPrefix)
Constructs a newThreadReferencingThreadFactory
specifying the prefix for the name of newly created threads.ThreadReferencingThreadFactory(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 newThreadReferencingThreadFactory
allowing you to provide specific values for everything which this class allows to be configured.ThreadReferencingThreadFactory(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
Constructs a newThreadReferencingThreadFactory
specifying anThread.UncaughtExceptionHandler
that will be provided to all newly created threads.ThreadReferencingThreadFactory(java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
Constructs a newThreadReferencingThreadFactory
specifying aConsumer
that will be provided threads as they created.ThreadReferencingThreadFactory(ExceptionHandler defaultThreadlyExceptionHandler)
Constructs a newThreadReferencingThreadFactory
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 ThreadReferencingThreadFactory.ThreadReferencingThreadFactoryBuilder
builder()
Construct a new builder as an alternative to the large full parameter constructor when multiple features are needing to be configured.java.util.List<java.lang.Thread>
getThreads(boolean requireAlive)
Get a list of currently known threads.java.lang.Thread
newThread(java.lang.Runnable r)
-
-
-
Constructor Detail
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory()
Constructs a newThreadReferencingThreadFactory
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 otherThreadReferencingThreadFactory
instances.
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(java.lang.String threadNamePrefix, boolean appendPoolIdToPrefix)
Constructs a newThreadReferencingThreadFactory
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
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(boolean useDaemonThreads)
Constructs a newThreadReferencingThreadFactory
specifying the behavior for if threads should be daemon or not.- Parameters:
useDaemonThreads
-true
if produced threads should be daemon threads
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(int threadPriority)
Constructs a newThreadReferencingThreadFactory
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
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
Constructs a newThreadReferencingThreadFactory
specifying anThread.UncaughtExceptionHandler
that will be provided to all newly created threads.- Parameters:
defaultUncaughtExceptionHandler
-Thread.UncaughtExceptionHandler
to provide to newly created threads
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(ExceptionHandler defaultThreadlyExceptionHandler)
Constructs a newThreadReferencingThreadFactory
specifying anExceptionHandler
that will be provided to all newly created threads.- Parameters:
defaultThreadlyExceptionHandler
-ExceptionHandler
to provide to newly created threads
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
Constructs a newThreadReferencingThreadFactory
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
-
ThreadReferencingThreadFactory
public ThreadReferencingThreadFactory(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 newThreadReferencingThreadFactory
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 ThreadReferencingThreadFactory.ThreadReferencingThreadFactoryBuilder builder()
Construct a new builder as an alternative to the large full parameter constructor when multiple features are needing to be configured.See
ConfigurableThreadFactory.builder()
for a list of default values.- Returns:
- A new builder which can be configured then finally constructed using
build()
- Since:
- 5.39
-
getThreads
public java.util.List<java.lang.Thread> getThreads(boolean requireAlive)
Get a list of currently known threads. This provides the ability to check if the thread is still alive before it is added, but threads may die / stop while the result is building so inactive threads may still be returned.- Parameters:
requireAlive
-true
to only provide threads currently seen as alive- Returns:
- A list of known thread references still reachable
-
newThread
public java.lang.Thread newThread(java.lang.Runnable r)
- Specified by:
newThread
in interfacejava.util.concurrent.ThreadFactory
- Overrides:
newThread
in classConfigurableThreadFactory
-
-