Skip to content

Instantly share code, notes, and snippets.

@bskim45
Last active January 8, 2021 16:29
Show Gist options
  • Save bskim45/e6ab1ef46ed155535e8bdfed2e174ff2 to your computer and use it in GitHub Desktop.
Save bskim45/e6ab1ef46ed155535e8bdfed2e174ff2 to your computer and use it in GitHub Desktop.
Scala Parallel Collection with parallelism level
/** Parallel Collection with parallelism level
*
* Changes the task support of a parallel collection to use a fork-join pool
* with desired parallelism level
*
* Usage:
* {{{
* Seq(1, 2, 3, 4).par.withCores(Runtime.getRuntime.availableProcessors * 2).map(_ * 2).sum
* }}}
*
* @see [[scala.collection.parallel.TaskSupport]]
*/
implicit class ParExtensions[T](iter: ParIterable[T]) {
def withCores(n: Int): ParIterable[T] = {
iter.tasksupport = new ForkJoinTaskSupport(new ForkJoinPool(n))
iter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment