Scala 的执行上下文为
import scala.concurrent.ExecutionContext.Implicits.global
Ans Play 有自己的执行上下文
import play.api.libs.concurrent.Execution.Implicits.defaultContext
主要区别是什么,我们应该使用哪个以及在哪个场景中。
Scala 的执行上下文为
import scala.concurrent.ExecutionContext.Implicits.global
Ans Play 有自己的执行上下文
import play.api.libs.concurrent.Execution.Implicits.defaultContext
主要区别是什么,我们应该使用哪个以及在哪个场景中。
scala.concurrent.ExecutionContext.Implicits.global
(Scala std lib execution context)是标准scala库提供的执行上下文。这是一个特殊的 ForkJoinPool,它使用阻塞方法来处理潜在的阻塞代码,以便在池中生成新线程。你不应该在 play 应用程序中使用它,因为 play 无法控制它。其中play.api.libs.concurrent.Execution.Implicits.defaultContext
(播放执行上下文)使用actor dispatcher
. 这应该用于播放应用程序。此外,将阻塞调用卸载到除播放执行上下文之外的不同执行上下文也是一种很好的做法。这样可以避免播放应用程序进入饥饿状态。
播放执行上下文 implplay.api.libs.concurrent.Execution.Implicits.defaultContext
val appOrNull: Application = Play._currentApp
appOrNull match {
case null => common
case app: Application => app.actorSystem.dispatcher
}
private val common = ExecutionContext.fromExecutor(new ForkJoinPool())
当应用程序不为空时,它使用actorSystem.dispatcher
Scala 标准执行上下文。
val executor: Executor = es match {
case null => createExecutorService
case some => some
}
此方法在考虑available processors
和读取配置的情况下创建执行器服务。
def createExecutorService: ExecutorService = {
def getInt(name: String, default: String) = (try System.getProperty(name, default) catch {
case e: SecurityException => default
}) match {
case s if s.charAt(0) == 'x' => (Runtime.getRuntime.availableProcessors * s.substring(1).toDouble).ceil.toInt
case other => other.toInt
}
def range(floor: Int, desired: Int, ceiling: Int) = scala.math.min(scala.math.max(floor, desired), ceiling)
val desiredParallelism = range(
getInt("scala.concurrent.context.minThreads", "1"),
getInt("scala.concurrent.context.numThreads", "x1"),
getInt("scala.concurrent.context.maxThreads", "x1"))
val threadFactory = new DefaultThreadFactory(daemonic = true)
try {
new ForkJoinPool(
desiredParallelism,
threadFactory,
uncaughtExceptionHandler,
true) // Async all the way baby
} catch {
case NonFatal(t) =>
System.err.println("Failed to create ForkJoinPool for the default ExecutionContext, falling back to ThreadPoolExecutor")
t.printStackTrace(System.err)
val exec = new ThreadPoolExecutor(
desiredParallelism,
desiredParallelism,
5L,
TimeUnit.MINUTES,
new LinkedBlockingQueue[Runnable],
threadFactory
)
exec.allowCoreThreadTimeOut(true)
exec
}
}
此代码负责托管阻塞。尝试blocking
在代码中遇到时创建一个新线程。
// Implement BlockContext on FJP threads
class DefaultThreadFactory(daemonic: Boolean) extends ThreadFactory with ForkJoinPool.ForkJoinWorkerThreadFactory {
def wire[T <: Thread](thread: T): T = {
thread.setDaemon(daemonic)
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler)
thread
}
def newThread(runnable: Runnable): Thread = wire(new Thread(runnable))
def newThread(fjp: ForkJoinPool): ForkJoinWorkerThread = wire(new ForkJoinWorkerThread(fjp) with BlockContext {
override def blockOn[T](thunk: =>T)(implicit permission: CanAwait): T = {
var result: T = null.asInstanceOf[T]
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker {
@volatile var isdone = false
override def block(): Boolean = {
result = try thunk finally { isdone = true }
true
}
override def isReleasable = isdone
})
result
}
})
}