绝对地。使用 Play Framework 在单独的 ThreadPool(也称为 ExecutionContext)中设置计算非常容易。您可能想阅读此处的文档,但简而言之,您将希望在Application.scala
控制器文件中执行类似的操作(注意此示例使用 Scala):
// Async Action that's triggered when a user clicks "Start Game".
// Runs logic in separate gameLogicContext thread pool and asynchronously returns a response without blocking of Play's default thread pool.
def startGame = Action.async { implicit request =>
Future {
// ... your game logic here. This will be run in gameLogicContext
Ok("Game started in separate thread pool") // http response
}(Contexts.gameLogicContext) // the thread pool the future should run in.
}
application.conf
然后你将在你的文件中设置一个单独的 gameLogicContext 线程池:
play {
akka {
actor {
game-logic-context = {
fork-join-executor {
parallelism-min = 300
parallelism-max = 300 // thread pool with 300 threads
}
}
}
}
}