Created
October 21, 2009 07:42
-
-
Save dcbriccetti/214957 to your computer and use it in GitHub Desktop.
This file contains 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
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