我想用一次创建的线程修复线程池。所以,我创建自己的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
}
//////
}
}
}
我无法理解这种行为。我认为,创建线程是昂贵的操作。
它在任务用完时不会停止执行程序。关闭仅在该调度程序的最后一个actor停止并且超时已经过去(调度程序配置上为shutdown-timeout
)且未启动分配给调度程序的新actor时运行。
对于具有许多短期演员的调度程序的用例和没有演员运行的时间段> 1秒这是默认值,您可以将设置调整为更高的值以使执行程序保持活动状态。
您可以粘贴主应用程序代码,在没有任务可用时终止。
如果你创建ActorSystem
然后除非你终止它,你的应用程序将不会退出,因为它确实创建了一些用户线程,使您的应用程序保持运行。