About ThreadFactory

java.util.concurrent.ThreadFactory itself is an interface. Its implementations are objects that create new threads on demand. Using thread factories eliminates the need of calls to new Thread.

The simplest implementation of this interface is just

class SimpleThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) { return new Thread(r);

Java provides defaultThreadFactory method of java.util.concurrent.Executors as one of ThreadFactory implementations. Actually, if you don’t provide your own implementation of ThreadFactory when instantiating an Executor defaultThreadFactory will be used. Its implementation is shown above.

Using ThreadFactory gives you an extra flexibility in dealing with Thread Pools. For example, if you want all the threads to be set as daemons, you may use

class DaemonThreadFactory implements ThreadFactory{
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
return t;

and provide an instance of DaemonThreadFactory as an argument for appropriate static method of Executors class

Leave a Reply

Your email address will not be published. Required fields are marked *