Top Description Inners Fields Constructors Methods
org.apache.commons.lang3.concurrent

public Class BasicThreadFactory

extends Object
implements ThreadFactory
Class Inheritance
All Implemented Interfaces
java.util.concurrent.ThreadFactory
Imports
java.lang.Thread.UncaughtExceptionHandler, java.util.Objects, java.util.concurrent.ExecutorService, .Executors, .ThreadFactory, java.util.concurrent.atomic.AtomicLong

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:

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);
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
BasicThreadFactory.Builder

A builder class for creating instances of BasicThreadFactory.

Field Summary

Modifier and TypeField 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
uncaughtExceptionHandler

Stores the uncaught exception handler.

private final ThreadFactory
wrappedFactory

Stores the wrapped factory.

Constructor Summary

AccessConstructor and Description
private
BasicThreadFactory(final BasicThreadFactory.Builder
the Builder object
builder
)

Creates a new instance of ThreadFactory and configures it from the specified Builder object.

Method Summary

Modifier and TypeMethod and Description
public static BasicThreadFactory.Builder

Returns:

a new builder.
builder
()

Creates a new builder.

public final Boolean

Returns:

the daemon flag
getDaemonFlag
()

Gets the daemon flag.

public final String

Returns:

the naming pattern
getNamingPattern
()

Gets the naming pattern for naming newly created threads.

public final Integer

Returns:

the priority for newly created threads
getPriority
()

Gets the priority of the threads created by this factory.

public long

Returns:

the number of threads created by this factory
getThreadCount
()

Gets the number of threads this factory has already created.

public final Thread.UncaughtExceptionHandler
getUncaughtExceptionHandler()

Gets the UncaughtExceptionHandler for the threads created by this factory.

public final ThreadFactory

Returns:

the wrapped ThreadFactory
getWrappedFactory
()

Gets the wrapped ThreadFactory.

private void
initializeThread(final Thread
the thread to be initialized
thread
)

Initializes the specified thread.

public Thread

Returns:

the newly created thread
newThread
(final Runnable
the Runnable to be executed by the new thread
runnable
)

Implements java.util.concurrent.ThreadFactory.newThread.

Creates a new thread.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

daemonback to summary
private final Boolean daemon

Stores the daemon status flag.

namingPatternback to summary
private final String namingPattern

Stores the naming pattern for newly created threads.

priorityback to summary
private final Integer priority

Stores the priority.

threadCounterback to summary
private final AtomicLong threadCounter

A counter for the threads created by this factory.

uncaughtExceptionHandlerback to summary
private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler

Stores the uncaught exception handler.

wrappedFactoryback to summary
private final ThreadFactory wrappedFactory

Stores the wrapped factory.

Constructor Detail

BasicThreadFactoryback to summary
private BasicThreadFactory(final BasicThreadFactory.Builder builder)

Creates a new instance of ThreadFactory and configures it from the specified Builder object.

Parameters
builder:BasicThreadFactory.Builder

the Builder object

Method Detail

builderback to summary
public static BasicThreadFactory.Builder builder()

Creates a new builder.

Returns:BasicThreadFactory.Builder

a new builder.

Since
3.18.0
getDaemonFlagback 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 setDaemon(true) on the newly created threads. Result can be null if no daemon flag was provided at creation time.

Returns:Boolean

the daemon flag

getNamingPatternback 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.

Returns:String

the naming pattern

getPriorityback 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.

Returns:Integer

the priority for newly created threads

getThreadCountback 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 newThread(Runnable) method is invoked.

Returns:long

the number of threads created by this factory

getUncaughtExceptionHandlerback to summary
public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()

Gets the UncaughtExceptionHandler for the threads created by this factory. Result can be null if no handler was provided.

Returns:Thread.UncaughtExceptionHandler

the UncaughtExceptionHandler

getWrappedFactoryback to summary
public final ThreadFactory getWrappedFactory()

Gets the wrapped 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.

Returns:ThreadFactory

the wrapped ThreadFactory

initializeThreadback to summary
private void initializeThread(final Thread thread)

Initializes the specified thread. This method is called by 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.

Parameters
thread:Thread

the thread to be initialized

newThreadback to summary
public Thread newThread(final Runnable runnable)

Implements java.util.concurrent.ThreadFactory.newThread.

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:Runnable

the Runnable to be executed by the new thread

Returns:Thread

the newly created thread

Annotations
@Override
org.apache.commons.lang3.concurrent back to summary

public Class BasicThreadFactory.Builder

extends Object
implements Builder<BasicThreadFactory>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.lang3.concurrent.BasicThreadFactory.Builder
All Implemented Interfaces
org.apache.commons.lang3.builder.Builder

A builder class for creating instances of 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.

Field Summary

Modifier and TypeField and Description
private Boolean
daemon

The daemon flag.

private Thread.UncaughtExceptionHandler
exceptionHandler

The uncaught exception handler.

private ThreadFactory
factory

The wrapped factory.

private String
namingPattern

The naming pattern.

private Integer
priority

The priority.

Constructor Summary

AccessConstructor and Description
public
Builder()

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public BasicThreadFactory

Returns:

the new BasicThreadFactory
build
()

Implements org.apache.commons.lang3.builder.Builder.build.

Creates a new BasicThreadFactory with all configuration options that have been specified by calling methods on this builder.

public BasicThreadFactory.Builder

Returns:

a reference to this Builder
daemon
()

Sets the daemon flag for the new BasicThreadFactory to true causing a new thread factory to create daemon threads.

public BasicThreadFactory.Builder

Returns:

a reference to this Builder
daemon
(final boolean
the value of the daemon flag
daemon
)

Sets the daemon flag for the new BasicThreadFactory.

public BasicThreadFactory.Builder

Returns:

a reference to this Builder
namingPattern
(final String
the naming pattern (must not be null)
namingPattern
)

Sets the naming pattern to be used by the new BasicThreadFactory.

public BasicThreadFactory.Builder

Returns:

a reference to this Builder
priority
(final int
the priority
priority
)

Sets the priority for the threads created by the new BasicThreadFactory.

public void
reset()

Resets this builder.

public BasicThreadFactory.Builder

Returns:

a reference to this Builder
uncaughtExceptionHandler
(final Thread.UncaughtExceptionHandler
the UncaughtExceptionHandler (must not be null)
exceptionHandler
)

Sets the uncaught exception handler for the threads created by the new BasicThreadFactory.

public BasicThreadFactory.Builder

Returns:

a reference to this Builder
wrappedFactory
(final ThreadFactory
the wrapped ThreadFactory (must not be null)
factory
)

Sets the ThreadFactory to be wrapped by the new BasicThreadFactory.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

daemonback to summary
private Boolean daemon

The daemon flag.

exceptionHandlerback to summary
private Thread.UncaughtExceptionHandler exceptionHandler

The uncaught exception handler.

factoryback to summary
private ThreadFactory factory

The wrapped factory.

namingPatternback to summary
private String namingPattern

The naming pattern.

priorityback to summary
private Integer priority

The priority.

Constructor Detail

Builderback to summary
public Builder()

Constructs a new instance.

Annotations
@Deprecated

Method Detail

buildback to summary
public BasicThreadFactory build()

Implements org.apache.commons.lang3.builder.Builder.build.

Creates a new BasicThreadFactory with all configuration options that have been specified by calling methods on this builder. After creating the factory reset() is called.

Returns:BasicThreadFactory

the new BasicThreadFactory

Annotations
@Override
daemonback to summary
public BasicThreadFactory.Builder daemon()

Sets the daemon flag for the new BasicThreadFactory to true causing a new thread factory to create daemon threads.

Returns:BasicThreadFactory.Builder

a reference to this Builder

Since
3.18.0
daemonback to summary
public BasicThreadFactory.Builder daemon(final boolean daemon)

Sets the daemon flag for the new BasicThreadFactory. If this flag is set to true the new thread factory will create daemon threads.

Parameters
daemon:boolean

the value of the daemon flag

Returns:BasicThreadFactory.Builder

a reference to this Builder

namingPatternback to summary
public BasicThreadFactory.Builder namingPattern(final String namingPattern)

Sets the naming pattern to be used by the new BasicThreadFactory.

Parameters
namingPattern:String

the naming pattern (must not be null)

Returns:BasicThreadFactory.Builder

a reference to this Builder

Exceptions
NullPointerException:
if the naming pattern is null
priorityback to summary
public BasicThreadFactory.Builder priority(final int priority)

Sets the priority for the threads created by the new BasicThreadFactory.

Parameters
priority:int

the priority

Returns:BasicThreadFactory.Builder

a reference to this Builder

resetback to summary
public void reset()

Resets this builder. All configuration options are set to default values.

Note

If the build() method was called, it is not necessary to call reset() explicitly because this is done automatically.

uncaughtExceptionHandlerback to summary
public BasicThreadFactory.Builder uncaughtExceptionHandler(final Thread.UncaughtExceptionHandler exceptionHandler)

Sets the uncaught exception handler for the threads created by the new BasicThreadFactory.

Parameters
exceptionHandler:Thread.UncaughtExceptionHandler

the UncaughtExceptionHandler (must not be null)

Returns:BasicThreadFactory.Builder

a reference to this Builder

Exceptions
NullPointerException:
if the exception handler is null
wrappedFactoryback to summary
public BasicThreadFactory.Builder wrappedFactory(final ThreadFactory factory)

Sets the ThreadFactory to be wrapped by the new BasicThreadFactory.

Parameters
factory:ThreadFactory

the wrapped ThreadFactory (must not be null)

Returns:BasicThreadFactory.Builder

a reference to this Builder

Exceptions
NullPointerException:
if the passed in ThreadFactory is null