相关疑难解决方法(0)

为什么阻止未来被视为不良做法?

我试图理解声明背后的理性 对于绝对需要阻止的情况,可以阻止期货(虽然不鼓励)

背后的想法ForkJoinPool是加入阻止操作的过程,这是期货和参与者的执行者上下文的主要实现.它应该有效阻止连接.

在这个非常简单的场景中,我写了一个小基准,看起来像旧式期货(scala 2.9)快2倍.

@inline
  def futureResult[T](future: Future[T]) = Await.result(future, Duration.Inf)

  @inline
  def futureOld[T](body: => T)(implicit  ctx:ExecutionContext): () => T = {
    val f = future(body)
    () => futureResult(f)
  }

  def main(args: Array[String]) {
    @volatile

    var res = 0d
    CommonUtil.timer("res1") {
      (0 until 100000).foreach {  i =>
       val f1 = futureOld(math.exp(1))
        val f2 = futureOld(math.exp(2))
        val f3 = futureOld(math.exp(3))
        res = res + f1() + f2() + f3()
      }
    }
    println("res1 = "+res)
    res = 0

    res …
Run Code Online (Sandbox Code Playgroud)

scala future nonblocking scala-2.10

5
推荐指数
2
解决办法
1148
查看次数

标签 统计

future ×1

nonblocking ×1

scala ×1

scala-2.10 ×1