Executors中:
<span style="white-space:pre"> </span>/** * Creates a thread pool that creates new threads as needed, but * will reuse previously constructed threads when they are * available, and uses the provided * ThreadFactory to create new threads when needed. * @param threadFactory the factory to use when creating new threads * @return the newly created thread pool * @throws NullPointerException if threadFactory is null */
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }//根据一个线程工厂创建一个线程缓冲池,线程池中的线程都由该线程工厂创建
import java.util.concurrent.ThreadFactory; public class DaemonThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); return t; } }
</pre><pre name="code" class="java">import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class DaemonFromFactory implements Runnable { @Override public void run() { try{ while(true){ TimeUnit.MILLISECONDS.sleep(100); System.out.println(Thread.currentThread()+" "+this); } }catch(Exception e){ System.out.println("Interrupted"); } } public static void main(String[] args) throws Exception { long start = System.currentTimeMillis(); ExecutorService exec = Executors.newCachedThreadPool(new DaemonThreadFactory()); for(int i=0;i<10;i++) exec.execute(new DaemonFromFactory()); System.out.println("All daemons started"); TimeUnit.MILLISECONDS.sleep(500); System.out.println("time="+(System.currentTimeMillis()-start)); } }
输出:
All daemons started Thread[Thread-6,5,main] thread.DaemonFromFactory@6fb104b6 Thread[Thread-5,5,main] thread.DaemonFromFactory@60469460 Thread[Thread-7,5,main] thread.DaemonFromFactory@1467bfc6 Thread[Thread-4,5,main] thread.DaemonFromFactory@8df59a3 Thread[Thread-3,5,main] thread.DaemonFromFactory@6fbe8b4c Thread[Thread-2,5,main] thread.DaemonFromFactory@196406c7 Thread[Thread-1,5,main] thread.DaemonFromFactory@3c907261 Thread[Thread-0,5,main] thread.DaemonFromFactory@7b3fb7f0 Thread[Thread-9,5,main] thread.DaemonFromFactory@692e1ca8 Thread[Thread-8,5,main] thread.DaemonFromFactory@48df2951 Thread[Thread-6,5,main] thread.DaemonFromFactory@6fb104b6 Thread[Thread-4,5,main] thread.DaemonFromFactory@8df59a3 Thread[Thread-5,5,main] thread.DaemonFromFactory@60469460 Thread[Thread-7,5,main] thread.DaemonFromFactory@1467bfc6 Thread[Thread-3,5,main] thread.DaemonFromFactory@6fbe8b4c Thread[Thread-2,5,main] thread.DaemonFromFactory@196406c7 Thread[Thread-8,5,main] thread.DaemonFromFactory@48df2951 Thread[Thread-9,5,main] thread.DaemonFromFactory@692e1ca8 Thread[Thread-0,5,main] thread.DaemonFromFactory@7b3fb7f0 Thread[Thread-1,5,main] thread.DaemonFromFactory@3c907261 Thread[Thread-2,5,main] thread.DaemonFromFactory@196406c7 Thread[Thread-3,5,main] thread.DaemonFromFactory@6fbe8b4c Thread[Thread-7,5,main] thread.DaemonFromFactory@1467bfc6 Thread[Thread-5,5,main] thread.DaemonFromFactory@60469460 Thread[Thread-6,5,main] thread.DaemonFromFactory@6fb104b6 Thread[Thread-1,5,main] thread.DaemonFromFactory@3c907261 Thread[Thread-4,5,main] thread.DaemonFromFactory@8df59a3 Thread[Thread-0,5,main] thread.DaemonFromFactory@7b3fb7f0 Thread[Thread-9,5,main] thread.DaemonFromFactory@692e1ca8 Thread[Thread-8,5,main] thread.DaemonFromFactory@48df2951 Thread[Thread-2,5,main] thread.DaemonFromFactory@196406c7 Thread[Thread-3,5,main] thread.DaemonFromFactory@6fbe8b4c Thread[Thread-6,5,main] thread.DaemonFromFactory@6fb104b6 Thread[Thread-7,5,main] thread.DaemonFromFactory@1467bfc6 Thread[Thread-5,5,main] thread.DaemonFromFactory@60469460 Thread[Thread-8,5,main] thread.DaemonFromFactory@48df2951 Thread[Thread-9,5,main] thread.DaemonFromFactory@692e1ca8 Thread[Thread-0,5,main] thread.DaemonFromFactory@7b3fb7f0 Thread[Thread-1,5,main] thread.DaemonFromFactory@3c907261 Thread[Thread-4,5,main] thread.DaemonFromFactory@8df59a3 time=507