在内部,newSingleThreadExecutor()
和newFixedThreadPool()
创建ThreadPoolExecutor
对象,然后newSingleThreadExecutor()
和newFixedThreadPool(1)
有什么区别?为什么Java有这些方法?
newFixedThreadPoo
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}