Skip to content

Instantly share code, notes, and snippets.

@ljwagerfield
Last active February 19, 2016 16:39
Show Gist options
  • Save ljwagerfield/7c86ec330219f8970de3 to your computer and use it in GitHub Desktop.
Save ljwagerfield/7c86ec330219f8970de3 to your computer and use it in GitHub Desktop.
def run() = {
import scala.concurrent.{ blocking, ExecutionContext, Future }
implicit val ec = ExecutionContext.fromExecutor(java.util.concurrent.Executors.newFixedThreadPool(1))
val startTime = System.nanoTime()
def event(name: String) =
Console.println(s"t=${(System.nanoTime - startTime) / 1000000000}s | $name")
event("REPORT request RCVD")
val candidates = 1 to 10
val calculations = candidates.foldLeft(Future.successful(List.empty[Int]))((pf, c) => pf.flatMap(p => Future {
Thread.sleep(1000)
event("REPORT processing")
p ::: List(c * c)
}))
calculations.map { calcs =>
event("REPORT response SENT")
}
event("PING request RCVD")
Future {
event("PING response SENT")
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment