Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Last active December 20, 2015 07:49
Show Gist options
  • Select an option

  • Save solidsnack/6096424 to your computer and use it in GitHub Desktop.

Select an option

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.
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