Last active
December 20, 2015 07:49
-
-
Save solidsnack/6096424 to your computer and use it in GitHub Desktop.
Demo of concurrent, side-effecting operations in Scala with a configurable thread pool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent._ | |
| import scala.concurrent.duration._ | |
| import java.util.concurrent.Executors.newFixedThreadPool | |
| val logger = java.util.logging.Logger.getLogger("par") | |
| def f(n: Int): Int = { | |
| Thread.sleep(20000) | |
| logger.info(f"$n%04d") | |
| n*2 | |
| } | |
| val e = newFixedThreadPool(8) | |
| val ctx: ExecutionContext = ExecutionContext.fromExecutorService(e) | |
| val futures = for (x <- 1 to 10) yield future(f(x))(ctx) | |
| val res = futures.map(Await.result(_, 10.minute)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment