针对ExecutorService产生的Thread线程池的Thread命名,采用Thread.setName无效; 需要采用ThreadFactory实现
提供一个实现demo:
class NamedThreadFactory implements ThreadFactory{
private static AtomicInteger tag = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("resumption调度器线程:"+ tag.getAndIncrement());
return thread;
}
}
private ExecutorService pool = Executors.newFixedThreadPool(perCount, new NamedThreadFactory());
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}