Scala的执行上下文为
import scala.concurrent.ExecutionContext.Implicits.global
Ans Play有自己的执行上下文
import play.api.libs.concurrent.Execution.Implicits.defaultContext
主要区别是什么?我们应该使用哪一个,哪个是哪个。
答案 0 :(得分:2)
scala.concurrent.ExecutionContext.Implicits.global
(Scala std lib执行上下文)是标准scala库提供的执行上下文。它是一个特殊的ForkJoinPool,它使用阻塞方法来处理可能阻塞的代码,以便在池中生成新线程。你不应该在播放应用程序中使用它,因为播放将无法控制它。play.api.libs.concurrent.Execution.Implicits.defaultContext
(播放执行上下文)使用actor dispatcher
的位置。这是播放应用程序应该使用的内容。除了播放执行上下文之外,最好将阻塞调用卸载到不同的执行上下文。这样就可以避免播放应用程序进入饥饿状态。
播放执行上下文impl play.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())
当app不为null时,它使用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
}
})
}