Class ThreadFactoryBuilderImpl
- All Implemented Interfaces:
ThreadFactoryBuilder
ThreadFactoryBuilder
.- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: ThreadFactoryBuilderImpl.java 1118 2024-03-15 16:14:15Z tquadrat $
- Since:
- 0.1.0
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.lang.internal.ThreadFactoryBuilderImpl"
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
The implementation ofThreadFactory
that is returned byThreadFactoryBuilder.build()
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ClassLoader
The contextClassLoader
for the new threads.private boolean
Iftrue
, inherit initial values for inheritable thread-locals from the constructing thread, otherwise no initial values are inherited.private boolean
true
if the new threads are daemon thread,false
otherwise.private boolean
Flag that determines whether this thread factory creates virtual threads.private IntFunction
<String> The factory method for the thread names.private int
The priority for the new threads.private long
The desired stack size for the new threads.private ThreadGroup
The thread group for the new threads.private Thread.UncaughtExceptionHandler
The uncaught exception handler for the new threads. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofThreadFactoryBuilderImpl
. -
Method Summary
Modifier and TypeMethodDescriptionfinal ThreadFactory
build()
Creates the instance ofThreadFactory
.final ThreadFactoryBuilderImpl
setContextClassLoader
(ClassLoader contextClassLoader) Sets the context class loader for the new threads created by the new thread factory.final ThreadFactoryBuilderImpl
setDaemon
(boolean flag) Sets the flag whether the new thread created by the new thread factory should be a daemon thread.final ThreadFactoryBuilderImpl
setInheritThreadLocals
(boolean flag) Sets the flag that controls whether the new threads, created by the new thread factory, will suppress, or not, the inheriting of initial values for inheritable thread-local variables from the constructing thread.final ThreadFactoryBuilderImpl
setNameFactory
(IntFunction<String> nameFactory) Sets the factory for the name of the threads created by the new thread factory.final ThreadFactoryBuilderImpl
setPriority
(int priority) Sets the priority for the new threads created by the new thread factory.final ThreadFactoryBuilderImpl
setStackSize
(long stackSize) Sets the stack size for the new threads created by the new thread factory.final ThreadFactoryBuilderImpl
setThreadGroup
(ThreadGroup threadGroup) Sets the thread group for the new threads created by the new thread factory.final ThreadFactoryBuilderImpl
setUncaughtExceptionHandler
(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets theUncaughtExceptionHandler
for the new threads created by the new thread factory.final ThreadFactoryBuilderImpl
setVirtual
(boolean flag) Determines whether the threads generated by this thread factory will be virtual threads.final String
toString()
-
Field Details
-
m_ContextClassLoader
The contextClassLoader
for the new threads. The default isnull
. -
m_InheritThreadLocals
Iftrue
, inherit initial values for inheritable thread-locals from the constructing thread, otherwise no initial values are inherited. The default istrue
. -
m_IsDaemon
true
if the new threads are daemon thread,false
otherwise. The default isfalse
. -
m_IsVirtual
Flag that determines whether this thread factory creates virtual threads.- Since:
- 0.4.5
-
m_NameFactory
The factory method for the thread names. The default returns "
Thread-#
", where "#" stands for a counter. -
m_Priority
The priority for the new threads. The default value of -1 indicates that no priority will be set explicitly.
-
m_StackSize
The desired stack size for the new threads. The default value of zero indicates that this parameter is to be ignored. -
m_ThreadGroup
The thread group for the new threads. The default isnull
. -
m_UncaughtExceptionHandler
The uncaught exception handler for the new threads. The default isnull
.
-
-
Constructor Details
-
ThreadFactoryBuilderImpl
public ThreadFactoryBuilderImpl()Creates a new instance ofThreadFactoryBuilderImpl
.
-
-
Method Details
-
build
Creates the instance ofThreadFactory
.- Specified by:
build
in interfaceThreadFactoryBuilder
- Returns:
- The thread factory.
-
setContextClassLoader
Sets the context class loader for the new threads created by the new thread factory.
The context class loader allows the creator of the thread to provide the appropriate class loader, through
Thread.getContextClassLoader()
, to code running in the thread when loading classes and resources.If not set, new threads will use the system class loader (or, failing that, the bootstrap class loader).
- Specified by:
setContextClassLoader
in interfaceThreadFactoryBuilder
- Parameters:
contextClassLoader
- The context classloader.- Returns:
- The builder.
- See Also:
-
setInheritThreadLocals
Sets the flag that controls whether the new threads, created by the new thread factory, will suppress, or not, the inheriting of initial values for inheritable thread-local variables from the constructing thread. This allows for finer grain control over inheritable thread-locals. Care must be taken when setting the flag to
false
, as it may lead to unexpected behavior if the new thread executes code that expects a specific thread-local value to be inherited.If not set, the value for the new threads will be
true
.- Specified by:
setInheritThreadLocals
in interfaceThreadFactoryBuilder
- Parameters:
flag
-true
if the values for inheritable thread locals will be inherited,false
to disable this feature.- Returns:
- The builder.
- See Also:
-
setDaemon
Sets the flag whether the new thread created by the new thread factory should be a daemon thread. Daemon threads will be aborted automatically when the last non-daemon thread terminates.
If not set, the new threads are not daemon threads.
- Specified by:
setDaemon
in interfaceThreadFactoryBuilder
- Parameters:
flag
-true
if the new threads are daemon threads,false
if they are regular threads.- Returns:
- The builder.
- See Also:
-
setNameFactory
Sets the factory for the name of the threads created by the new thread factory.
The name factory method will be call with a numeric argument that is unique for each invocation by the new thread factory; in fact, it is a counter value.
If not set, a name factory is used that returns names looking like this: "
Thread-#
" where the hash symbol will be replaced by the counter value.- Specified by:
setNameFactory
in interfaceThreadFactoryBuilder
- Parameters:
nameFactory
- The name factory.- Returns:
- The builder.
-
setPriority
Sets the priority for the new threads created by the new thread factory. Proper values are -1 (to indicate that the priority should not be set explicitly) and numbers between 1 to 10, inclusively.
If not set, the value will be -1.
If the thread factory will not be configured explicitly with a priority, the newly created threads will get their priority set equal to the priority of the thread calling
ThreadFactory.newThread(Runnable)
, creating a new thread. The methodThread.setPriority(int)
may be used to change the priority to a new value.- Specified by:
setPriority
in interfaceThreadFactoryBuilder
- Parameters:
priority
- The priority value.- Returns:
- The builder.
- Throws:
ValidationException
- The given value is not valid.- See Also:
-
setStackSize
Sets the stack size for the new threads created by the new thread factory. The stack size is the approximate number of bytes of address space that the virtual machine is to allocate for the new thread's stack. The effect of the
stackSize
parameter, if any, is highly platform dependent.On some platforms, specifying a higher value for the
stackSize
parameter may allow a thread to achieve greater recursion depth before throwing aStackOverflowError
. Similarly, specifying a lower value may allow a greater number of threads to exist concurrently without throwing anOutOfMemoryError
(or other internal error). The details of the relationship between the value of thestackSize
parameter and the maximum recursion depth and concurrency level are platform-dependent. On some platforms, the value of thestackSize
parameter may have no effect whatsoever.The virtual machine is free to treat the stackSize parameter as a suggestion. If the specified value is unreasonably low for the platform, the virtual machine may instead use some platform-specific minimum value; if the specified value is unreasonably high, the virtual machine may instead use some platform-specific maximum. Likewise, the virtual machine is free to round the specified value up or down as it sees fit (or to ignore it completely).
Specifying a value of zero for the
stackSize
parameter will cause the new tread factory to not set the stack size for the new threads explicitly.- Specified by:
setStackSize
in interfaceThreadFactoryBuilder
- Parameters:
stackSize
- The stack size for the new threads.- Returns:
- The builder.
- See Also:
-
setThreadGroup
Sets the thread group for the new threads created by the new thread factory.
If no thread group will be specified, the thread group for threads created by the new thread factory is set to the thread group of the thread that called .
ThreadFactory.newThread(Runnable)
.- Specified by:
setThreadGroup
in interfaceThreadFactoryBuilder
- Parameters:
threadGroup
- The thread group.- Returns:
- The builder.
- See Also:
-
setUncaughtExceptionHandler
public final ThreadFactoryBuilderImpl setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets the
UncaughtExceptionHandler
for the new threads created by the new thread factory.No handler will be set if none is specified for the new factory.
- Specified by:
setUncaughtExceptionHandler
in interfaceThreadFactoryBuilder
- Parameters:
uncaughtExceptionHandler
- The uncaught exception handler.- Returns:
- The builder.
- See Also:
-
setVirtual
Determines whether the threads generated by this thread factory will be virtual threads.- Specified by:
setVirtual
in interfaceThreadFactoryBuilder
- Parameters:
flag
-true
if the generated threads are virtual, otherwisefalse
.
-
toString
-