public class BasicThreadFactory
extends java.lang.Object
implements java.util.concurrent.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() .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 |
---|---|
static class |
BasicThreadFactory.Builder
A builder class for creating instances of
BasicThreadFactory . |
Modifier and Type | Field and Description |
---|---|
private java.lang.Boolean |
daemonFlag
Stores the daemon status flag.
|
private java.lang.String |
namingPattern
Stores the naming pattern for newly created threads.
|
private java.lang.Integer |
priority
Stores the priority.
|
private java.util.concurrent.atomic.AtomicLong |
threadCounter
A counter for the threads created by this factory.
|
private java.lang.Thread.UncaughtExceptionHandler |
uncaughtExceptionHandler
Stores the uncaught exception handler.
|
private java.util.concurrent.ThreadFactory |
wrappedFactory
Stores the wrapped factory.
|
Modifier | Constructor and Description |
---|---|
private |
BasicThreadFactory(BasicThreadFactory.Builder builder)
Creates a new instance of
ThreadFactoryImpl and configures it
from the specified Builder object. |
Modifier and Type | Method and Description |
---|---|
java.lang.Boolean |
getDaemonFlag()
Returns the daemon flag.
|
java.lang.String |
getNamingPattern()
Returns the naming pattern for naming newly created threads.
|
java.lang.Integer |
getPriority()
Returns the priority of the threads created by this factory.
|
long |
getThreadCount()
Returns the number of threads this factory has already created.
|
java.lang.Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
Returns the
UncaughtExceptionHandler for the threads created by
this factory. |
java.util.concurrent.ThreadFactory |
getWrappedFactory()
Returns the wrapped
ThreadFactory . |
private void |
initializeThread(java.lang.Thread t)
Initializes the specified thread.
|
java.lang.Thread |
newThread(java.lang.Runnable r)
Creates a new thread.
|
private final java.util.concurrent.atomic.AtomicLong threadCounter
private final java.util.concurrent.ThreadFactory wrappedFactory
private final java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler
private final java.lang.String namingPattern
private final java.lang.Integer priority
private final java.lang.Boolean daemonFlag
private BasicThreadFactory(BasicThreadFactory.Builder builder)
ThreadFactoryImpl
and configures it
from the specified Builder
object.builder
- the Builder
objectpublic final java.util.concurrent.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
public final java.lang.String getNamingPattern()
public final java.lang.Boolean getDaemonFlag()
setDaemon(true)
on the newly created threads. Result can be
null if no daemon flag was provided at creation time.public final java.lang.Integer getPriority()
public final java.lang.Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
UncaughtExceptionHandler
for the threads created by
this factory. Result can be null if no handler was provided.UncaughtExceptionHandler
public long getThreadCount()
newThread(Runnable)
method is invoked.public java.lang.Thread newThread(java.lang.Runnable r)
newThread
in interface java.util.concurrent.ThreadFactory
r
- the Runnable
to be executed by the new threadprivate void initializeThread(java.lang.Thread t)
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.t
- the thread to be initialized