【发布时间】:2019-07-13 22:54:38
【问题描述】:
我希望有一个固定的线程池,其中包含一次创建的线程。所以,我创建了自己的ExecutorServiceConfigurator:
class FixedThreadPoolExecutorServiceConfigurator(config: Config, prerequisites: DispatcherPrerequisites) extends ExecutorServiceConfigurator(config, prerequisites) {
class ThreadPoolExecutorServiceFactory extends ExecutorServiceFactory {
def createExecutorService: ExecutorService = {
Executors.newFixedThreadPool(40)
}
}
private val executor = new ThreadPoolExecutorServiceFactory()
override def createExecutorServiceFactory(id: String, threadFactory: ThreadFactory): ExecutorServiceFactory = {
executor
}
}
并使用它:
blocking-dispatcher {
type = Dispatcher
executor = "abc.FixedThreadPoolExecutorServiceConfigurator"
throughput = 1
thread-pool-executor {
fixed-pool-size = 60
}
}
但是每次,当我的程序没有任何任务时,Akka 都会关闭 ExecutorService:
akka.dispatch.MessageDispatcher:
private val shutdownAction = new Runnable {
@tailrec
final def run(): Unit = {
shutdownSchedule match {
case SCHEDULED ⇒
try {
if (inhabitants == 0) shutdown() //Warning, racy
}
//////
}
}
}
我无法理解这种行为。我认为,创建线程是一项昂贵的操作。
【问题讨论】:
标签: scala akka threadpool executorservice akka-dispatcher