Class ThreadFactoryBuilderImpl
- All Implemented Interfaces:
ThreadFactoryBuilder
ThreadFactoryBuilder.- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: ThreadFactoryBuilderImpl.java 1143 2024-08-10 18:53:55Z 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 classThe implementation ofThreadFactorythat is returned byThreadFactoryBuilder.build(). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ClassLoaderThe contextClassLoaderfor the new threads.private booleanIftrue, inherit initial values for inheritable thread-locals from the constructing thread, otherwise no initial values are inherited.private booleantrueif the new threads are daemon thread,falseotherwise.private booleanFlag that determines whether this thread factory creates virtual threads.private IntFunction<String> The factory method for the thread names.private intThe priority for the new threads.private longThe desired stack size for the new threads.private ThreadGroupThe thread group for the new threads.private Thread.UncaughtExceptionHandlerThe uncaught exception handler for the new threads. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofThreadFactoryBuilderImpl. -
Method Summary
Modifier and TypeMethodDescriptionfinal ThreadFactorybuild()Creates the instance ofThreadFactory.final ThreadFactoryBuilderImplsetContextClassLoader(ClassLoader contextClassLoader) Sets the context class loader for the new threads created by the new thread factory.final ThreadFactoryBuilderImplsetDaemon(boolean flag) Sets the flag whether the new thread created by the new thread factory should be a daemon thread.final ThreadFactoryBuilderImplsetInheritThreadLocals(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 ThreadFactoryBuilderImplsetNameFactory(IntFunction<String> nameFactory) Sets the factory for the name of the threads created by the new thread factory.final ThreadFactoryBuilderImplsetPriority(int priority) Sets the priority for the new threads created by the new thread factory.final ThreadFactoryBuilderImplsetStackSize(long stackSize) Sets the stack size for the new threads created by the new thread factory.final ThreadFactoryBuilderImplsetThreadGroup(ThreadGroup threadGroup) Sets the thread group for the new threads created by the new thread factory.final ThreadFactoryBuilderImplsetUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets theUncaughtExceptionHandlerfor the new threads created by the new thread factory.final ThreadFactoryBuilderImplsetVirtual(boolean flag) Determines whether the threads generated by this thread factory will be virtual threads.final StringtoString()
-
Field Details
-
m_ContextClassLoader
The contextClassLoaderfor 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
trueif the new threads are daemon thread,falseotherwise. 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:
buildin 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:
setContextClassLoaderin 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:
setInheritThreadLocalsin interfaceThreadFactoryBuilder- Parameters:
flag-trueif the values for inheritable thread locals will be inherited,falseto 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:
setDaemonin interfaceThreadFactoryBuilder- Parameters:
flag-trueif the new threads are daemon threads,falseif 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:
setNameFactoryin 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:
setPriorityin 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
stackSizeparameter, if any, is highly platform dependent.On some platforms, specifying a higher value for the
stackSizeparameter 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 thestackSizeparameter and the maximum recursion depth and concurrency level are platform-dependent. On some platforms, the value of thestackSizeparameter 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
stackSizeparameter will cause the new tread factory to not set the stack size for the new threads explicitly.- Specified by:
setStackSizein 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:
setThreadGroupin interfaceThreadFactoryBuilder- Parameters:
threadGroup- The thread group.- Returns:
- The builder.
- See Also:
-
setUncaughtExceptionHandler
public final ThreadFactoryBuilderImpl setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets the
UncaughtExceptionHandlerfor the new threads created by the new thread factory.No handler will be set if none is specified for the new factory.
- Specified by:
setUncaughtExceptionHandlerin 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:
setVirtualin interfaceThreadFactoryBuilder- Parameters:
flag-trueif the generated threads are virtual, otherwisefalse.
-
toString
-
