ExecutorService启动守护线程
2019-01-17 本文已影响2人
Woople
通常使用ExecutorService pool = Executors.newFixedThreadPool(1)
方式创建一个线程池,但是这个线程池里面的线程都是非守护线程,如何将线程设置为守护线程,下面代码参考apache shiro
ExecutorService pool = Executors.newFixedThreadPool(1,
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName(threadNamePrefix + thread.getId());
return thread;
}
});