Java tutorial
//package com.java2s; //License from project: Apache License import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.concurrent.*; public class Main { /** * Wrapper over newCachedThreadPool. Thread names are formatted as prefix-ID, where ID is a * unique, sequentially assigned integer. */ public ThreadPoolExecutor newDaemonCachedThreadPool(String prefix) { ThreadFactory threadFactory = namedThreadFactory(prefix); return (ThreadPoolExecutor) Executors.newCachedThreadPool(threadFactory); } /** * Create a cached thread pool whose max number of threads is `maxThreadNumber`. Thread names * are formatted as prefix-ID, where ID is a unique, sequentially assigned integer. */ public static ThreadPoolExecutor newDaemonCachedThreadPool(String prefix, int maxThreadNumber) { ThreadFactory threadFactory = namedThreadFactory(prefix); return new ThreadPoolExecutor(0, maxThreadNumber, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); } /** * Create a thread factory that names threads with a prefix and also sets the threads to daemon. */ public static ThreadFactory namedThreadFactory(String prefix) { return new ThreadFactoryBuilder().setDaemon(true).setNameFormat(prefix + "-%d").build(); } }