使用 Executors.newFixedThreadPool(1) 有意义吗??
是的。如果要按到达顺序处理所有提交的任务,这是有道理的
在两个线程(main + oneAnotherThread)的场景下,使用执行器服务效率高吗?通过调用 new Runnable(){ } 直接创建新线程是否比使用 ExecutorService 更好?
我更喜欢 ExecutorService 或 ThreadPoolExecutor,即使是 1 个线程。
请参阅下面的 SE 问题,了解 ThreadPoolExecutor 相对于新 Runnable() 的优势:
ExecutorService vs Casual Thread Spawner
在这种情况下使用 ExecutorService 有什么好处和坏处?
查看有关 ExexutorService 用例的相关 SE 问题:
Java's Fork/Join vs ExecutorService - when to use which?
关于您在主题行中的查询(来自grepcode),两者都是相同的:
newFixedThreadPool API 将返回 ThreadPoolExecutor 作为 ExecutorService:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
和
newSingleThreadExecutor() 将 ThreadPoolExecutor 作为 ExecutorService 返回:
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
我同意 @assylias 关于异同的回答。