Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Created October 21, 2009 07:42
Show Gist options
  • Save dcbriccetti/214957 to your computer and use it in GitHub Desktop.
Save dcbriccetti/214957 to your computer and use it in GitHub Desktop.
package org.talkingpuffin.util
import java.util.concurrent.{ExecutorCompletionService, Callable, Executors}
object Parallelizer {
/**
* Runs, in the number of threads requested, the function f, giving it each A of args, returning a List[T]
*/
def run[T,A](numThreads: Int, args: List[A], f: (A) => T): List[T] = {
val pool = Executors.newFixedThreadPool(numThreads)
val completionService = new ExecutorCompletionService[T](pool)
args.foreach(arg => completionService.submit(new Callable[T] {def call = f(arg)}))
val result = args map(_ => completionService.take.get)
pool.shutdown
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment