Java Executors newSingleThreadExecutor() 方法

2025年3月25日 | 1 分钟阅读

Executors 类的 newSingleThreadExecutor() 方法创建一个 Executor,该 Executor 使用一个单工作线程,并在一个无界队列上运行。(但请注意,如果此单线程在关闭前由于执行期间的故障而终止,则在需要执行后续任务时,将有一个新线程取代它。)

语法

参数

threadFactory - 创建新线程时使用的工厂

返回值

新创建的单线程 Executor

抛出

NullPointerException

示例 1

输出

current thread : main
 Executor Service...
Runnable...
Submit the task 
:: pool-1-thread-1

示例 1

输出

Shutingdown executor
Running Task!
下一主题Java Executors