简体   繁体   English

使用ConcurrentHashMap

[英]Using ConcurrentHashMap

I'm new to threading in Java and I need to access data structure from few active threads. 我是Java新线程的新手,我需要从几个活动线程访问数据结构。 I've heard that java.util.concurrent.ConcurrentHashMap is threading-friendly. 我听说java.util.concurrent.ConcurrentHashMap是线程友好的。 Do I need to use synchronized(map){} while accessing ConcurrentHashMap or it will handle locks itself? 访问ConcurrentHashMap时是否需要使用synchronized(map){}或者它本身会处理锁?

It handles the locks itself, and in fact you have no access to them (there is no other option) 它处理锁本身,实际上你无权访问它们(没有其他选择)

You can use synchronized in special cases for writes, but it is very rare that you should need to do this. 您可以在特殊情况下使用synchronized进行写入,但您很少需要执行此操作。 eg if you need to implement your own putIfAbsent because the cost of creating an object is high. 例如,如果您需要实现自己的putIfAbsent因为创建对象的成本很高。

Using syncrhonized for reads would defeat the purpose of using the concurrent collection. 使用syncrhonized进行读取会破坏使用并发集合的目的。

ConcurrentHashMap is suited only to the cases where you don't need any more atomicity than provided out-of-the-box. ConcurrentHashMap仅适用于您不需要比开箱即用提供的更多原子性的情况。 If for example you need to get a value, do something with it, and then set a new value, all in an atomic operation, this cannot be achieved without external locking. 例如,如果你需要获取一个值,用它做一些事情,然后设置一个新的值,所有这些都在原子操作中,没有外部锁定就无法实现。

In all such cases nothing can replace explicit locks in your code and it is nothing but waste to use this implementation instead of the basic HashMap . 在所有这些情况下,没有任何东西可以替代代码中的显式锁,并且使用此实现而不是基本的HashMap只是浪费。

Short answer: no you don't need to use synchronized(map) . 简答:你不需要使用synchronized(map)
Long answer: 答案很长:

  • all the operations provided by ConcurrentHashMap are thread safe and you can call them without worrying about locking ConcurrentHashMap提供的所有操作都是线程安全的,您可以调用它们而不必担心锁定
  • however, if you need some operations to be atomic in your code, you will still need some sort of locking at the client side 但是,如果您需要某些操作在代码中是原子操作,则仍需要在客户端进行某种锁定

No, you don't need, but if you need to depend on internal synchronization, you should use Collections.synchronizedMap instead. 不,您不需要,但如果您需要依赖内部同步,则应使用Collections.synchronizedMap From the javadoc of ConcurrentHashMap : ConcurrentHashMap的javadoc:

This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details. 在依赖于线程安全但不依赖于其同步细节的程序中,此类可与Hashtable完全互操作。

Actually it won't synchronize on the whole data structure but on subparts (some buckets) of it. 实际上它不会在整个数据结构上同步,而是在它的子部分(一些桶)上同步。 This implies that ConcurrentHashMap 's iterators are weakly consistent and the size of the map can be inaccurate. 这意味着ConcurrentHashMap的迭代器非常一致,并且映射的大小可能不准确。 (But on the other hand it's put and get operations are still consistent and the throughput is higher) (但另一方面,它的放置和获取操作仍然一致,吞吐量更高)

There is one more important feature to note for concurrenthmp other than the concurrency feature it provides, which is fail safe iterator. 除了它提供的并发功能之外,concurrenthmp还有一个值得注意的重要功能 ,即故障安全迭代器。 Use CHMP just because they want to edit the entryset for put/remove while iteration. 使用CHMP只是因为他们想要在迭代时编辑put / remove的入口集。 Collections.synchronizedMap(Map) is other one. Collections.synchronizedMap(Map)是另一个。 But ConcurrentModificationException may come in the above case. 但是ConcurrentModificationException可能出现在上面的情况中。

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

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