关于 Executors.newSingleThreadExecutor()
本以为只是一个简单的单线程线程池,与 Executors.newFixedThreadExecutor(1)
类似,但它的 DOC
注释中又显式提到:
Unlike the otherwise equivalent {@code newFixedThreadPool(1)} the returned executor is guaranteed not to be reconfigurable to use additional threads.
与类似功能的
newFixedThreadPool(1)
不一样,该方法返回的线程池执行器不可以被重新配置以使用额外的线程。
关键在于不可以重新配置。来看看它的构造器:
1 | public static ExecutorService newSingleThreadExecutor() { |
1 | /** |
1 | /** |
再看看 ThreadPoolExecutor
的实现:👇
1 | /** |
那我们试试,reconfigurable newSingleThreadExecutor
会怎样:👇
1 |
|