java 多线程环境下的Hashmap和Hashtable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1361784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hashmap and hashtable in multithreaded environment
提问by Paul Whelan
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 个线程会同时更新其值,对吗?
回答by Paul Whelan
Look at ConcurrentHashMaps 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. You can even configure the number of buckets :) Tweaking this can help performance depending on your data.
性能是通过而不是使用地图范围的锁来获得的,该集合默认维护一个包含 16 个锁的列表,每个锁用于锁定地图的单个存储桶。您甚至可以配置存储桶的数量 :) 根据您的数据进行调整可以帮助提高性能。
I can't recommend enough Java Concurrency in Practice by Brian Goetz http://jcip.net/
我不能推荐足够多的实践中的 Java Concurrency by Brian Goetz http://jcip.net/
I still learn something new every time I read it.
每次阅读时,我仍然会学到一些新东西。
回答by Nettogrof
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 不是同步的,所以速度更快,但在多线程环境中可能会出现问题。
回答by Miserable Variable
Also note that Hashtable and Collections.synchronizedMapare safe only for individual operations. Any operations involving multiple keys or check-then-act that need to be atomic will notbe so and additional client side locking will be required.
还要注意 Hashtable 和Collections.synchronizedMap仅对单个操作是安全的。任何需要原子性的涉及多个键或检查然后操作的操作都不会如此,并且需要额外的客户端锁定。
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)append the parameter to value at a key:
appendToValue(Map, Object k1, String suffix)
交换两个不同键的值:
swapValues(Map, Object k1, Object k2)将参数附加到键的值:
appendToValue(Map, Object k1, String suffix)
And yes, all of this is covered in JCIP :-)
是的,所有这些都包含在 JCIP 中 :-)
回答by ATorras
Yes, all the methods are done atomically, but values() method not (see docs).
是的,所有方法都是以原子方式完成的,但 values() 方法不是(请参阅文档)。
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 包,它为多线程环境提供了非常好的控制和数据结构。
回答by Adrian Pronk
Hashtables are synchronized but they're an old implementation that you could almost say is deprecated. 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:
通过调用可以为 HashMap 获得类似级别的同步:
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 为线程安全映射提供了额外的用于原子更新的有用方法。

