简体   繁体   English

多线程环境中的Hashmap和哈希表

[英]Hashmap and hashtable in multithreaded environment

I am really confused on how these 2 collections behave in multithreaded environment. 我真的很困惑这两个集合在多线程环境中的表现。

Hash table is synchronized that means no 2 threads will be updating its value simultaneously right? 哈希表是同步的,这意味着没有2个线程同时更新它的值吗?

Look at ConcurrentHashMap s for Thread safe Maps. 查看用于线程安全映射的ConcurrentHashMap

They offer all the features of HashTable with a performance very close to a HashMap. 它们提供HashTable的所有功能,其性能非常接近HashMap。

Performance is gained by instead of using a map wide lock, the collection maintains a list of 16 locks by default, each of which is used to lock a single bucket of the map. 通过代替使用地图宽锁获得性能,该集合默认维护一个包含16个锁的列表,每个锁用于锁定地图的单个存储桶。 You can even configure the number of buckets :) Tweaking this can help performance depending on your data. 您甚至可以配置存储桶数量:)根据您的数据调整此数据可以提高性能。

I can't recommend enough Java Concurrency in Practice by Brian Goetz http://jcip.net/ 我不能在Brian Goetz的http://jcip.net/中推荐足够的Java Concurrency in Practice

I still learn something new every time I read it. 每当我读到它时,我仍然会学到新东西。

Also note that Hashtable and Collections.synchronizedMap are safe only for individual operations. 另请注意,Hashtable和Collections.synchronizedMap仅对单个操作是安全的。 Any operations involving multiple keys or check-then-act that need to be atomic will not be so and additional client side locking will be required. 任何涉及需要原子的多个键或check-then-act的操作都不会如此,并且需要额外的客户端锁定。

For example, you cannot write any of the following methods without additional locking: 例如,如果没有额外的锁定,则无法编写以下任何方法:

  • swap the values at two different keys: swapValues(Map, Object k1, Object k2) 交换两个不同键的值: swapValues(Map, Object k1, Object k2)

  • append the parameter to value at a key: appendToValue(Map, Object k1, String suffix) 将参数附加到键的值: appendToValue(Map, Object k1, String suffix)

And yes, all of this is covered in JCIP :-) 是的,所有这些都在JCIP中涵盖:-)

Exactly, HashTable is synchronized that mean that it's safe to use it in multi-thread environment (many thread access the same HashTable) If two thread try to update the hashtable at the sametime, one of them will have to wait that the other thread finish his update. 确切地说,HashTable是同步的,这意味着在多线程环境中使用它是安全的(许多线程访问相同的HashTable)如果两个线程尝试在同一时间更新哈希表,其中一个将不得不等待另一个线程完成他的更新。

HashMap is not synchronized, so it's faster, but you can have problem in a multi-thread environment. HashMap未同步,因此速度更快,但您可能在多线程环境中遇到问题。

Yes, all the methods are done atomically, but values() method not (see docs ). 是的,所有方法都是原子方式完成的,但是value()方法不是(参见docs )。

Paul was faster than me recommending you the java.util.concurrent package, which gives you a very fine control and data structures for multithreade environments. Paul比我推荐java.util.concurrent包更快,它为多线程环境提供了非常精细的控制和数据结构。

Hashtables are synchronized but they're an old implementation that you could almost say is deprecated. Hashtables是同步的,但它们是一个旧的实现,你几乎可以说它已被弃用。 Also, they don't allow null keys (maybe not null values either? not sure). 此外,他们不允许空键(也许不是空值?不确定)。

One problem is that although every method call is synchronized, most interesting actions require more than one call so you have to synchronize around the several calls. 一个问题是尽管每个方法调用都是同步的,但大多数有趣的操作需要多个调用,因此您必须围绕多个调用进行同步。

A similar level of synchronization can be obtained for HashMaps by calling: 通过调用以下内容,可以为HashMaps获得类似的同步级别:

Map m = Collections.synchronizedMap(new HashMap());

which wraps a map in synchronized method calls. 它在同步方法调用中包装映射。 But this has the same concurrency drawbacks as Hashtable. 但这与Hashtable具有相同的并发缺陷。

As Paul says, ConcurrentHashMaps provide thread safe maps with additional useful methods for atomic updates. 正如Paul所说,ConcurrentHashMaps为原子更新提供了其他有用的方法的线程安全映射。

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

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