Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Scala parallel collections better in some ways than the parallel collections already available in Java?

I've recently been learning about various libraries for concurrency in Java such as ConcurrentHashMap and the lovely non blocking one from Cliff Click

I don't know much about Scala but I've heard good things about the recent parallel collections library.

I'd like to know what are some major advantages that this library would give over Java based libraries?

like image 852
barrymac Avatar asked Jun 01 '11 16:06

barrymac


People also ask

What is parallel collection in Scala?

The design of Scala's parallel collections library is inspired by and deeply integrated with Scala's (sequential) collections library (introduced in 2.8). It provides a parallel counterpart to a number of important data structures from Scala's (sequential) collection library, including: ParArray. ParVector. mutable.

Is Scala parallel programming?

Scala programming language already brings us a collection to quickly implement parallel computing, the Parallel Collections. In this tutorial, we'll check out some concepts of parallelism with Scala and the usage of parallel collections.

What is concurrency in Scala?

Scala concurrency is built on top of the Java concurrency model. On Sun JVMs, with a IO-heavy workload, we can run tens of thousands of threads on a single machine. A Thread takes a Runnable. You have to call start on a Thread in order for it to run the Runnable.


1 Answers

The two collections are for different uses.

Java's concurrent collections allow you to use them from a parallel context: many threads can access them simultaneously, and the collection will be sure to do the right thing (so the callers don't have to worry about locks and such).

Scala's parallel collections, in contrast, are designed to run high-order operations on themselves without you having to worry about creating threads. So you can write something like:

myData.par.filter(_.expensiveTest()).map(_.expensiveComputation())

and the filter and map will each execute in parallel (but the filter will complete before the map starts).

like image 122
Rex Kerr Avatar answered Oct 11 '22 12:10

Rex Kerr