Tom
Tom

Reputation: 6332

import ExecutionContext.global and ExecutionContext.Implicits.global at the same time in scala

I have following test case:

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)

  }

There is compile error that complains Cannot find an implicit ExecutionContext

But If I remove import scala.concurrent.ExecutionContext.global from the code, then it works.

I would ask why it doesn't work if I import both ExecutionContext.global and ExecutionContext.Implicits.global

Upvotes: 3

Views: 1731

Answers (1)

Muhunthan
Muhunthan

Reputation: 413

Why don't you try this

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

Upvotes: 2

Related Questions