This should run in the current thread...
{
val currentThreadEx = new AbstractExecutorService {
override def execute(r: Runnable) { r.run }
override def shutdownNow(): java.util.List[Runnable] = new java.util.ArrayList[Runnable]()
override def shutdown() {}
override def isTerminated = false
override def isShutdown = false
override def awaitTermination(timeout: Long, unit: TimeUnit) = false
}
implicit val exContext = ExecutionContext.fromExecutor(currentThreadEx)
val f = Future {
10 + 1
}
println(Await.result(f, 1 seconds))
}
This will run on the default executor...
{
import ExecutionContext.Implicits.global
val f = Future {
10 + 1
}
println(Await.result(f, 1 seconds))
}
As you can see, you can use ExecutorService as the abstraction point.
Or you can use a function written in terms of Monads, and then you can bind those operations together without dealing with the context (i.e. Future).
def op1[M[_]: Monad](i: Int)(j: Int): M[Int] = {
Monad[M].point { i * j }
}
println(Monad[Id].point(10) >>= op1[Id](10))
println((Future { 10 } >>= op1[Future](10)).run)