org.apache.commons.lang3.concurrent
Class BasicThreadFactory

java.lang.Object
  extended by org.apache.commons.lang3.concurrent.BasicThreadFactory
All Implemented Interfaces:
ThreadFactory

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:

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
Version:
$Id: BasicThreadFactory.java 1079423 2011-03-08 16:38:09Z sebb $

Nested Class Summary
static class BasicThreadFactory.Builder
           A builder class for creating instances of BasicThreadFactory.
 
Method Summary
 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 r)
          Creates a new thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getWrappedFactory

public final ThreadFactory getWrappedFactory()
Returns 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:
the wrapped ThreadFactory

getNamingPattern

public final String getNamingPattern()
Returns the naming pattern for naming newly created threads. Result can be null if no naming pattern was provided.

Returns:
the naming pattern

getDaemonFlag

public final Boolean getDaemonFlag()
Returns 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:
the daemon flag

getPriority

public final Integer getPriority()
Returns the priority of the threads created by this factory. Result can be null if no priority was specified.

Returns:
the priority for newly created threads

getUncaughtExceptionHandler

public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Returns the UncaughtExceptionHandler for the threads created by this factory. Result can be null if no handler was provided.

Returns:
the UncaughtExceptionHandler

getThreadCount

public long getThreadCount()
Returns 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

newThread

public Thread newThread(Runnable r)
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.

Specified by:
newThread in interface ThreadFactory
Parameters:
r - the Runnable to be executed by the new thread
Returns:
the newly created thread


Copyright © 2001-2011 The Apache Software Foundation. All Rights Reserved.