Skip to content

Instantly share code, notes, and snippets.

@pkukielka
Last active December 11, 2015 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkukielka/4630522 to your computer and use it in GitHub Desktop.
Save pkukielka/4630522 to your computer and use it in GitHub Desktop.
package com.pkukielka
import scala.actors.Futures._
object ScalaSum {
def sum(r: Range): Long = { var i = r.start; var sum = 0L; while (i <= r.end) { i += r.step; sum += i }; return sum }
val cores = Runtime.getRuntime().availableProcessors()
val jobs = (1 to cores).map(_ to 1234567890 by cores).map(range => future(sum(range)))
def main(args: Array[String]) {
println(awaitAll(5000, jobs: _*).map(_.get.asInstanceOf[Long]).sum)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment