DaemonThreadFactory.java Source code

Java tutorial

Introduction

Here is the source code for DaemonThreadFactory.java

Source

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * Simple implementation of a ThreadFactory that 
 * marks all of the threads as daemon threads.
 */
public class DaemonThreadFactory implements ThreadFactory {
    private final ThreadFactory factory = Executors.defaultThreadFactory();

    /**
     * Create a new thread for the thread pool.  The create
     * thread will be a daemon thread.
     * 
     * @param r      The runnable used by the thread pool.
     * 
     * @return The newly created thread. 
     */
    public Thread newThread(Runnable r) {
        Thread t = factory.newThread(r);
        if (!t.isDaemon()) {
            t.setDaemon(true);
        }
        return t;
    }

}