Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need for ParHashMap from Scala while there is ConcurrentHashMap from Java

I considered two collections with a similar concept - ParHashMap from Scala and ConcurrentHashMap from Java. Both of them have the same time complexity and both of them are thread safe and lock-free, but they only are based on different concepts under the hood - trie and hash table accordingly. And this reasoning leads to question: why do we need for ParHashMap from Scala while there is ConcurrentHashMap from Java?

like image 957
pacman Avatar asked Sep 03 '25 16:09

pacman


1 Answers

ConcurrentHashMap is a thread safe Map<> implementation. If you have multiple threads accessing it at the same time they will be in sync.

ParHashMap is a parallel collection. If you execute operations here (like map(), filter(), aggregate()) Scala will parallelize it for you (similar to Spark but only within a single JVM).

To summarize, ConcurrentHashMap gives the primitive to synchronize threads for concurrency, ParHashMap takes care of both sync and execution.

Edit: Note that ParHashMap is not itself necessarily thread-safe. The idea is to call its methods from a single thread and let the parallelism be handled by the parallel data structure itself.

like image 133
marios Avatar answered Sep 05 '25 07:09

marios