public class BasicThreadFactory extends Object implements 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:
String.format() method. The string can contain the place holder %d
which will be replaced by the number of the current thread (ThreadFactoryImpl keeps 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.java.lang.Thread class defines constants for valid ranges of priority
values.UncaughtExceptionHandler for 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);
| Modifier and Type | Class and Description |
|---|---|
static class |
BasicThreadFactory.Builder
A builder class for creating instances of
BasicThreadFactory. |
| Modifier | Constructor and Description |
|---|---|
protected |
BasicThreadFactory(BasicThreadFactory.Builder aBuilder)
Creates a new instance of
ThreadFactoryImpl and configures it from
the specified Builder object. |
| Modifier and Type | Method and Description |
|---|---|
ETriState |
getDaemon()
Returns the daemon flag.
|
static Thread.UncaughtExceptionHandler |
getDefaultUncaughtExceptionHandler() |
String |
getNamingPattern()
Returns the naming pattern for naming newly created threads.
|
Integer |
getPriority()
Returns the priority of the threads created by this factory.
|
long |
getThreadCount()
Returns the number of threads this factory has already created.
|
Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
Returns the
UncaughtExceptionHandler for the threads created by
this factory. |
ThreadFactory |
getWrappedFactory()
Returns the wrapped
ThreadFactory. |
protected void |
initializeThread(Thread aThread)
Initializes the specified thread.
|
Thread |
newThread(Runnable aRunnable)
Creates a new thread.
|
static void |
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler aHdl)
Set the default uncaught exception handler for future instances of
BasicThreadFactory.
|
String |
toString() |
protected BasicThreadFactory(@Nonnull BasicThreadFactory.Builder aBuilder)
ThreadFactoryImpl and configures it from
the specified Builder object.aBuilder - the Builder objectpublic static void setDefaultUncaughtExceptionHandler(@Nonnull Thread.UncaughtExceptionHandler aHdl)
aHdl - The handlers to be used. May not be null.@Nonnull public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
null.@Nonnull public final ThreadFactory getWrappedFactory()
ThreadFactory. This factory is used for
actually creating threads. This method never returns null. If no
ThreadFactory was passed when this object was created, a default
thread factory is returned.ThreadFactory@Nonnull public final String getNamingPattern()
@Nonnull public final ETriState getDaemon()
setDaemon(true) on the newly created threads. Result can be
null if no daemon flag was provided at creation time.@Nullable public final Integer getPriority()
public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
UncaughtExceptionHandler for the threads created by
this factory. Result can be null if no handler was provided.UncaughtExceptionHandler@Nonnull public long getThreadCount()
newThread(Runnable) method is invoked.@OverrideOnDemand protected void initializeThread(@Nonnull Thread aThread)
newThread(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.aThread - the thread to be initialized@Nonnull public Thread newThread(@Nonnull Runnable aRunnable)
newThread in interface ThreadFactoryaRunnable - the Runnable to be executed by the new threadCopyright © 2014–2019 Philip Helger. All rights reserved.