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() .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 | Method and Description |
---|---|
Boolean |
getDaemonFlag()
Returns the daemon flag.
|
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 . |
Thread |
newThread(Runnable runnable)
Creates a new thread.
|
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
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()
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 Thread newThread(Runnable runnable)
newThread
in interface ThreadFactory
runnable
- the Runnable
to be executed by the new threadCopyright © 2001–2020 The Apache Software Foundation. All rights reserved.