本文主要是介绍newFixedThreadPool(1)和newSingleThreadExecutor()的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看看这两个方法的源码:
public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());}
public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));}
可以看到,这两个方法返回的对象不一样,一个是ThreadPoolExecutor,而另外一个是FinalizableDelegatedExecutorService
FinalizableDelegatedExecutorService 只有finalize方法; 没有其他什么内容。其继承了DelegatedExecutorService。
static class FinalizableDelegatedExecutorService extends Executors.DelegatedExecutorService {FinalizableDelegatedExecutorService(ExecutorService var1) {super(var1);}protected void finalize() {super.shutdown();}
}
最关键部分还是在DelegatedExecutorService类中。
这篇关于newFixedThreadPool(1)和newSingleThreadExecutor()的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!