public class BasicThreadFactory extends Object implements ThreadFactory
An implementation of the
ThreadFactory interface that provides some
configuration options for the threads it creates.
ThreadFactory is used for instance by an
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
%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.
java.lang.Threadclass defines constants for valid ranges of priority values.
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
BasicThreadFactory are not created directly, but the
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
// 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|
A builder class for creating instances of
|Modifier and Type||Method and Description|
Returns the daemon flag.
Returns the naming pattern for naming newly created threads.
Returns the priority of the threads created by this factory.
Returns the number of threads this factory has already created.
Returns the wrapped
Creates a new thread.
public final ThreadFactory getWrappedFactory()
ThreadFactory. This factory is used for actually creating threads. This method never returns null. If no
ThreadFactorywas passed when this object was created, a default thread factory is returned.
public final String getNamingPattern()
public final Boolean getDaemonFlag()
setDaemon(true)on the newly created threads. Result can be null if no daemon flag was provided at creation time.
public final Integer getPriority()
public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
UncaughtExceptionHandlerfor the threads created by this factory. Result can be null if no handler was provided.
public long getThreadCount()
newThread(Runnable)method is invoked.
Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.