BasicThreadFactory¶
- public class org.apache.commons.lang3.concurrent.BasicThreadFactory implements ThreadFactory[source]¶
An implementation of the
ThreadFactoryinterface that provides some configuration options for the threads it creates.A
ThreadFactoryis used for instance by anExecutorServiceto create the threads it uses for executing tasks. In many cases users do not have to care about aThreadFactorybecause the default one used by anExecutorServicewill do. However, if there are special requirements for the threads, a customThreadFactoryhas 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
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.
BasicThreadFactorywraps 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 defaultThreadFactoryis used.Instances of
BasicThreadFactoryare not created directly, but the nestedBuilderclass 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 aBasicThreadFactoryis created and installed in anExecutorService:// 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);
- Since:
3.0
Methods¶
Creates a new builder. |
|
Gets the daemon flag. |
|
Gets the naming pattern for naming newly created threads. |
|
Gets the priority of the threads created by this factory. |
|
Gets the number of threads this factory has already created. |
|
Gets the |
|
Gets the wrapped |
|
Creates a new thread. |
- public static BasicThreadFactory.Builder builder()[source]¶
Creates a new builder.
- Returns:
A new builder.
- Since:
3.18.0
- public final Boolean getDaemonFlag()[source]¶
Gets the daemon flag. This flag determines whether newly created threads should be daemon threads. If true, this factory object calls
setDaemon(true)on the newly created threads. Result can benullif no daemon flag was provided at creation time.- Returns:
The daemon flag.
- public final String getNamingPattern()[source]¶
Gets the naming pattern for naming newly created threads. Result can be
nullif no naming pattern was provided.The naming pattern is a
format stringthat expects a single argument. This argument is the number of the thread to be created. For instance, if the naming pattern is"MyThread-%d", the first thread created by this factory will be named"MyThread-1", the second one"MyThread-2"and so on.- Returns:
The naming pattern.
- public final Integer getPriority()[source]¶
Gets the priority of the threads created by this factory. Result can be
nullif no priority was specified.- Returns:
The priority for newly created threads.
- public long getThreadCount()[source]¶
Gets the number of threads this factory has already created. This class maintains an internal counter that is incremented each time the
#newThread(Runnable)method is invoked.- Returns:
The number of threads created by this factory.
- public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()[source]¶
Gets the
UncaughtExceptionHandlerfor the threads created by this factory. Result can benullif no handler was provided.- Returns:
- public final ThreadFactory getWrappedFactory()[source]¶
Gets the wrapped
ThreadFactory. This factory is used for actually creating threads. This method never returnsnull. If noThreadFactorywas passed when this object was created, a default thread factory is returned.- Returns:
The wrapped
ThreadFactory.
- @Override public Thread newThread(Runnable runnable)[source]¶
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.
- Parameters:
runnable – The
Runnableto be executed by the new thread.- Returns:
The newly created thread.