我想有一个固定的线程池和一旦创建的线程。所以,我创建了自己的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
}
//////
}
}
}
我无法理解这种行为。我认为,创建线程是昂贵的操作。