ExecutorService启动守护线程

通常使用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;  
        }  
    });     

猜你喜欢

转载自blog.csdn.net/weixin_34294649/article/details/87190322