简体   繁体   English

为什么我们需要Scala的ParHashMap,而Java却有ConcurrentHashMap

[英]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. 我考虑了两个具有类似概念的集合ParHashMap和Java的ConcurrentHashMap。 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? 这种推理导致了一个问题:为什么Java中有ConcurrentHashMap时,我们为什么需要Scala中的ParHashMap?

ConcurrentHashMap is a thread safe Map<> implementation. ConcurrentHashMap是线程安全的Map<>实现。 If you have multiple threads accessing it at the same time they will be in sync. 如果您有多个线程同时访问它,则它们将同步。

ParHashMap is a parallel collection. ParHashMap是一个并行集合。 If you execute operations here (like map() , filter() , aggregate() ) Scala will parallelize it for you (similar to Spark but only within a single JVM). 如果您在此处执行操作(例如map()filter()aggregate() ),Scala会为您并行化它(类似于Spark,但仅在单个JVM中)。

To summarize, ConcurrentHashMap gives the primitive to synchronize threads for concurrency, ParHashMap takes care of both sync and execution. 总而言之, ConcurrentHashMap提供了用于并发同步线程的原语, ParHashMap负责同步和执行。

Edit : Note that ParHashMap is not itself necessarily thread-safe. 编辑 :请注意, ParHashMap本身不一定是线程安全的。 The idea is to call its methods from a single thread and let the parallelism be handled by the parallel data structure itself. 想法是从单个线程调用其方法,并让并行性由并行数据结构本身处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM