Class BasicThreadFactory
- All Implemented Interfaces:
ThreadFactory
An implementation of the ThreadFactory interface that provides some configuration options
for the threads it creates.
A ThreadFactory is used for instance by an ExecutorService to create the threads
it uses for executing tasks. In many cases users do not have to care about a
ThreadFactory because the default one used by an ExecutorService will do.
However, if there are special requirements for the threads, a custom ThreadFactory has to
be created.
This class provides some frequently needed configuration options for the threads it creates. These are the following:
- A name pattern for the threads created by this factory can be specified. This is often useful
if an application uses multiple executor services for different purposes. If the names of the
threads used by these services have meaningful names, log output or exception traces can be much
easier to read. Naming patterns are format strings as used by the
String.format()method. The string can contain the place holder%dwhich will be replaced by the number of the current thread (ThreadFactoryImplkeeps a counter of the threads it has already created). For instance, the naming pattern"My %d. worker thread"will result in thread names like"My 1. worker thread","My 2. worker thread"and so on. - A flag whether the threads created by this factory should be daemon threads. This can impact the exit behavior of the current Java application because the JVM shuts down if there are only daemon threads running.
- The priority of the thread. Here an integer value can be provided. The
java.lang.Threadclass defines constants for valid ranges of priority values. - The
UncaughtExceptionHandlerfor the thread. This handler is called if an uncaught exception occurs within the thread.
BasicThreadFactory wraps another thread factory which actually creates new threads. The
configuration options are set on the threads created by the wrapped thread factory. On
construction time the factory to be wrapped can be specified. If none is provided, a default
ThreadFactory is used.
Instances of BasicThreadFactory are not created directly, but the nested Builder
class is used for this purpose. Using the builder only the configuration options an application
is interested in need to be set. The following example shows how a BasicThreadFactory is
created and installed in an ExecutorService:
// Create a factory that produces daemon threads with a naming pattern and
// a priority
BasicThreadFactory factory = new BasicThreadFactory.Builder ().setNamingPattern ("workerthread-%d")
.setDaemon (true)
.setPriority (Thread.MAX_PRIORITY)
.build ();
// Create an executor service for single-threaded execution
ExecutorService exec = Executors.newSingleThreadExecutor (factory);
- Since:
- 8.5.3
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBasicThreadFactory(@NonNull BasicThreadFactoryBuilder aBuilder) Creates a new instance ofThreadFactoryImpland configures it from the specifiedBuilderobject. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull BasicThreadFactoryBuilderbuilder()final @NonNull ETriStateReturns the daemon flag.static @NonNull Thread.UncaughtExceptionHandlerfinal @NonNull StringReturns the naming pattern for naming newly created threads.final @Nullable IntegerReturns the priority of the threads created by this factory.longReturns the number of threads this factory has already created.Returns theUncaughtExceptionHandlerfor the threads created by this factory.final @NonNull ThreadFactoryReturns the wrappedThreadFactory.protected voidinitializeThread(@NonNull Thread aThread) Initializes the specified thread.@NonNull ThreadCreates a new thread.static voidSet the default uncaught exception handler for future instances of BasicThreadFactory.toString()
-
Constructor Details
-
BasicThreadFactory
Creates a new instance ofThreadFactoryImpland configures it from the specifiedBuilderobject.- Parameters:
aBuilder- theBuilderobject
-
-
Method Details
-
setDefaultUncaughtExceptionHandler
public static void setDefaultUncaughtExceptionHandler(@NonNull Thread.UncaughtExceptionHandler aHdl) Set the default uncaught exception handler for future instances of BasicThreadFactory. By default a logging exception handler is present.- Parameters:
aHdl- The handlers to be used. May not benull.- Since:
- 9.0.0
-
getDefaultUncaughtExceptionHandler
- Returns:
- The default uncaught exception handler used. Never
null.
-
getWrappedFactory
Returns the wrappedThreadFactory. This factory is used for actually creating threads. This method never returns null. If noThreadFactorywas passed when this object was created, a default thread factory is returned.- Returns:
- the wrapped
ThreadFactory
-
getNamingPattern
Returns the naming pattern for naming newly created threads. Result can be null if no naming pattern was provided.- Returns:
- the naming pattern
-
getDaemon
Returns the daemon flag. This flag determines whether newly created threads should be daemon threads. If true, this factory object callssetDaemon(true)on the newly created threads. Result can be null if no daemon flag was provided at creation time.- Returns:
- the daemon flag
-
getPriority
Returns the priority of the threads created by this factory. Result can be null if no priority was specified.- Returns:
- the priority for newly created threads
-
getUncaughtExceptionHandler
Returns theUncaughtExceptionHandlerfor the threads created by this factory. Result can be null if no handler was provided.- Returns:
- the
UncaughtExceptionHandler
-
getThreadCount
public long getThreadCount()Returns the number of threads this factory has already created. This class maintains an internal counter that is incremented each time thenewThread(Runnable)method is invoked.- Returns:
- the number of threads created by this factory
-
initializeThread
Initializes the specified thread. This method is called bynewThread(Runnable)after a new thread has been obtained from the wrapped thread factory. It initializes the thread according to the options set for this factory.- Parameters:
aThread- the thread to be initialized
-
newThread
Creates a new thread. This implementation delegates to the wrapped factory for creating the thread. Then, on the newly created thread the corresponding configuration options are set.- Specified by:
newThreadin interfaceThreadFactory- Parameters:
aRunnable- theRunnableto be executed by the new thread- Returns:
- the newly created thread
-
toString
-
builder
- Returns:
- A new
BasicThreadFactoryBuilderinstance. Nevernull.
-