1:Executor线程池
// Start worker thread pool
private val threadPool = {
val threadFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("Executor task launch worker-%d")
.setThreadFactory(new ThreadFactory {
override def newThread(r: Runnable): Thread =
// Use UninterruptibleThread to run tasks so that we can allow running codes without being
// interrupted by `Thread.interrupt()`. Some issues, such as KAFKA-1894, HADOOP-10622,
// will hang forever if some methods are interrupted.
new UninterruptibleThread(r, "unused") // thread name will be set by ThreadFactoryBuilder
})
.build()
Executors.newCachedThreadPool(threadFactory).asInstanceOf[ThreadPoolExecutor]
}
进入Executors —>Alt + 7查看类中方法

本文深入探讨了Java中Executor线程池的实现细节,包括如何使用ThreadFactory自定义线程名称,以及通过UninterruptibleThread避免任务因中断而挂起的问题。文章提供了具体的代码示例,展示了如何创建和配置一个高性能的线程池。
711





