【问题标题】:Why Akka shutdowns dispatcher, when there is no tasks?没有任务时,为什么 Akka 会关闭调度程序?
【发布时间】: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


    【解决方案1】:

    您能否粘贴在没有可用任务时终止的主应用程序代码。

    如果您正在创建ActorSystem,那么除非您终止它,否则您的应用将不会退出,因为它确实会创建一些用户线程来保持您的应用运行。

    【讨论】:

    • 我没有主应用程序代码,当没有可用任务时终止。它只是 Akka http 应用程序,因此应用程序在我停止之前一直处于活动状态。
    【解决方案2】:

    它不会在执行器用完任务时停止它。仅当该调度程序的最后一个 Actor 停止并且超时(调度程序配置上的shutdown-timeout)并且没有分配给调度程序的新 Actor 启动时,才会运行关闭。

    对于调度程序的用例,其中包含许多短生命周期的参与者,并且没有运行参与者的时间超过 1 秒,这是默认值,您可以将设置调整为更高的值以保持执行程序的活动。

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 2015-05-24
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多