2016-10-28 56 views
0

斯卡拉之间的差异有其执行上下文作为是什么Scala的执行上下文和游戏的执行上下文

import scala.concurrent.ExecutionContext.Implicits.global 

答打法有自己的执行上下文

import play.api.libs.concurrent.Execution.Implicits.defaultContext 

请告诉我的主要区别是哪一个我们是否应该使用和在哪个senario中。

回答

2

scala.concurrent.ExecutionContext.Implicits.global(斯卡拉标准库执行上下文)是标准斯卡拉库提供的执行上下文。这是一个特殊的ForkJoinPool,它使用阻塞方法来处理潜在的阻塞代码,以便在池中产生新线程。您不应该在播放应用程序中使用它,因为播放无法控制它。 play.api.libs.concurrent.Execution.Implicits.defaultContext(Play执行上下文)使用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()) 

当应用程序不为空,它使用actorSystem.dispatcher

斯卡拉标准执行上下文

播放执行上下文。

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 
     } 
    }) 
    }