Reputation: 396
I know basic difference between the Akka dispatcher vs Global Execution context.
I tried this code with scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global
val resultF = (0 to 100).map(
x =>
Future {
val startTime = System.currentTimeMillis()
val task = Future {
Thread.sleep(100)
x
}
task.onSuccess {
case result =>
val timeRemaining = System.currentTimeMillis() - startTime
println(s"$result $timeRemaining")
}
}
)
StdIn.readLine()
The Above code prints on an average the time equal to Thread.sleep(), around 103 milliseconds on an average.
However following code prints the time taken between 100-400 millisecond.
val system = ActorSystem("test")
implicit val executionContext = system.dispatcher
val resultF = (0 to 100).map(
x =>
Future {
val startTime = System.currentTimeMillis()
val task = Future {
Thread.sleep(100)
x
}
task.onSuccess {
case result =>
val timeRemaining = System.currentTimeMillis() - startTime
println(s"$result $timeRemaining")
}
}
)
StdIn.readLine()
I'm failing to understand what's the major difference apart from thread-pool
used.
Upvotes: 2
Views: 3066
Reputation: 15464
I'm failing to understand what's the major difference apart from thread-pool used.
The only difference is the thread pools used and their work scheduling algorithms.
The Akka ActorSystem threadpool is a fork-join executor, as described at http://doc.akka.io/docs/akka/2.5/scala/dispatchers.html
The "global" default ExecutionContext is a work-stealing thread pool, see e.g. https://github.com/scala/scala/blob/v2.12.3/src/library/scala/concurrent/ExecutionContext.scala#L135
See e.g. How is the fork/join framework better than a thread pool?
Upvotes: 3