在scala中同时导入ExecutionContext.global和ExecutionContext.Implicits.global

时间:2018-05-04 07:46:28

标签: scala

我有以下测试用例:

test("test future") {
    import scala.concurrent.ExecutionContext.global
    import scala.concurrent.ExecutionContext.Implicits.global
    case class Result(value: String)
    val f = Future {
      println("Start future")
      Thread.sleep(1000)
      println("End future")
      Result("The Job is finished")
    }

    f.onComplete({
      case Success(r) => println(r.value)
      case Failure(ex) => ex.printStackTrace()
    })


    //Hold the test thread
    Thread.sleep(3000)

  }

抱怨Cannot find an implicit ExecutionContext

的编译错误

但如果我从代码中删除import scala.concurrent.ExecutionContext.global, 那就行了。

如果我同时导入ExecutionContext.globalExecutionContext.Implicits.global

,我会问为什么它不起作用

1 个答案:

答案 0 :(得分:1)

你为什么不试试这个

import scala.concurrent.ExecutionContext.Implicits.{ global => ImplicitsGlobal }
import scala.concurrent.ExecutionContext.global