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.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()
.namingPattern("workerthread-%d")
.daemon(true)
.priority(Thread.MAX_PRIORITY)
.build();
// Create an executor service for single-threaded execution
ExecutorService exec = Executors.newSingleThreadExecutor(factory);
| Modifier and Type | Class and Description |
|---|---|
| public static class | BasicThreadFactory.
A builder class for creating instances of |
| Modifier and Type | Field and Description |
|---|---|
| private final Boolean | daemon
Stores the daemon status flag. |
| private final String | namingPattern
Stores the naming pattern for newly created threads. |
| private final Integer | priority
Stores the priority. |
| private final AtomicLong | threadCounter
A counter for the threads created by this factory. |
| private final Thread. | uncaughtExceptionHandler
Stores the uncaught exception handler. |
| private final ThreadFactory | wrappedFactory
Stores the wrapped factory. |
| Access | Constructor and Description |
|---|---|
| private | BasicThreadFactory(final BasicThreadFactory.
the builderBuilder objectCreates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public static BasicThreadFactory. | |
| public final Boolean | |
| public final String | Returns: the naming patternGets the naming pattern for naming newly created threads. |
| public final Integer | Returns: the priority for newly created threadsGets the priority of the threads created by this factory. |
| public long | Returns: the number of threads created by this factoryGets the number of threads this factory has already created. |
| public final Thread. | Returns: theUncaughtExceptionHandlerGets the |
| public final ThreadFactory | |
| private void | |
| public Thread | Returns: the newly created threadthe runnable)Runnable to be executed by the new threadImplements java. Creates a new thread. |
| daemon | back to summary |
|---|---|
| private final Boolean daemon Stores the daemon status flag. | |
| namingPattern | back to summary |
|---|---|
| private final String namingPattern Stores the naming pattern for newly created threads. | |
| priority | back to summary |
|---|---|
| private final Integer priority Stores the priority. | |
| threadCounter | back to summary |
|---|---|
| private final AtomicLong threadCounter A counter for the threads created by this factory. | |
| uncaughtExceptionHandler | back to summary |
|---|---|
| private final Thread. Stores the uncaught exception handler. | |
| wrappedFactory | back to summary |
|---|---|
| private final ThreadFactory wrappedFactory Stores the wrapped factory. | |
| BasicThreadFactory | back to summary |
|---|---|
| private BasicThreadFactory(final BasicThreadFactory. Creates a new instance of
| |
| builder | back to summary |
|---|---|
| public static BasicThreadFactory. Creates a new builder.
| |
| getDaemonFlag | back to summary |
|---|---|
| public final Boolean getDaemonFlag() Gets the daemon flag. This flag determines whether newly created
threads should be daemon threads. If true, this factory object
calls
| |
| getNamingPattern | back to summary |
|---|---|
| public final String getNamingPattern() Gets the naming pattern for naming newly created threads. Result can be null if no naming pattern was provided.
| |
| getPriority | back to summary |
|---|---|
| public final Integer getPriority() Gets the priority of the threads created by this factory. Result can be null if no priority was specified.
| |
| getThreadCount | back to summary |
|---|---|
| public long getThreadCount() Gets the number of threads this factory has already created. This
class maintains an internal counter that is incremented each time the
| |
| getUncaughtExceptionHandler | back to summary |
|---|---|
| public final Thread. Gets the | |
| getWrappedFactory | back to summary |
|---|---|
| public final ThreadFactory getWrappedFactory() Gets the wrapped
| |
| initializeThread | back to summary |
|---|---|
| private void initializeThread(final Thread thread) Initializes the specified thread. This method is called by
| |
| newThread | back to summary |
|---|---|
| public Thread newThread(final Runnable runnable) Implements java. 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. | |
BasicThreadFactory.
Using this builder class instances of BasicThreadFactory can be
created and initialized. The class provides methods that correspond to
the configuration options supported by BasicThreadFactory. Method
chaining is supported. Refer to the documentation of BasicThreadFactory for a usage example.
| Modifier and Type | Field and Description |
|---|---|
| private Boolean | daemon
The daemon flag. |
| private Thread. | exceptionHandler
The uncaught exception handler. |
| private ThreadFactory | factory
The wrapped factory. |
| private String | namingPattern
The naming pattern. |
| private Integer | priority
The priority. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public BasicThreadFactory | Returns: the newBasicThreadFactoryImplements org. Creates a new |
| public BasicThreadFactory. | Returns: a reference to thisBuilderSets the daemon flag for the new |
| public BasicThreadFactory. | Returns: a reference to thisBuilderthe value of the daemon flag daemon)Sets the daemon flag for the new |
| public BasicThreadFactory. | Returns: a reference to thisBuilderthe naming pattern (must not be null) namingPattern)Sets the naming pattern to be used by the new |
| public BasicThreadFactory. | |
| public void | |
| public BasicThreadFactory. | Returns: a reference to thisBuilderthe exceptionHandlerUncaughtExceptionHandler (must not be
null)Sets the uncaught exception handler for the threads created by the
new |
| public BasicThreadFactory. | Returns: a reference to thisBuilderthe wrapped factory)ThreadFactory (must not be
null)Sets the |
| daemon | back to summary |
|---|---|
| private Boolean daemon The daemon flag. | |
| exceptionHandler | back to summary |
|---|---|
| private Thread. The uncaught exception handler. | |
| factory | back to summary |
|---|---|
| private ThreadFactory factory The wrapped factory. | |
| namingPattern | back to summary |
|---|---|
| private String namingPattern The naming pattern. | |
| priority | back to summary |
|---|---|
| private Integer priority The priority. | |
| Builder | back to summary |
|---|---|
| public Builder()
Deprecated Constructs a new instance.
| |
| build | back to summary |
|---|---|
| public BasicThreadFactory build() Implements org. Creates a new
| |
| daemon | back to summary |
|---|---|
| public BasicThreadFactory. Sets the daemon flag for the new
| |
| daemon | back to summary |
|---|---|
| public BasicThreadFactory. Sets the daemon flag for the new
| |
| namingPattern | back to summary |
|---|---|
| public BasicThreadFactory. Sets the naming pattern to be used by the new
| |
| priority | back to summary |
|---|---|
| public BasicThreadFactory. Sets the priority for the threads created by the new
| |
| reset | back to summary |
|---|---|
| public void reset() Resets this builder. All configuration options are set to default values. Note If the | |
| uncaughtExceptionHandler | back to summary |
|---|---|
| public BasicThreadFactory. Sets the uncaught exception handler for the threads created by the
new
| |
| wrappedFactory | back to summary |
|---|---|
| public BasicThreadFactory. Sets the
| |