score:1

Accepted answer

in the absence of any code, i'll provide a simple version but give the caveat that this is an inaccurate way of measuring and not great for things that take a small amount of time. only really useful if the operation your measuring actually takes a decent amount of time so most of the time is spent in the future and not in the test code.

import scala.concurrent._
import scala.concurrent.duration._
import executioncontext.implicits.global

val mylist = (1 to 10)

val start = system.currenttimemillis

val seqfutures = for (elem <- mylist) yield {
    future {
      thread.sleep(5000) // blocking operation
    }
}

val result = future.sequence(seqfutures)

await.result(result, 30.seconds)
val end = system.currenttimemillis

println(s"${end-start} seconds")

if you are going to have a large number of blocking io calls, it can be worth configuring a separate execution context that has a larger thread pool just for the blocking io to avoid the blocking io consuming all your threads.


Related Query