Java中的哈希表和同步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19234719/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:18:14  来源:igfitidea点击:

hashtable and synchronization in Java

javamultithreadingsynchronizationhashtable

提问by brain storm

I have read that hashtables are thread-safe because it is synchronized. consider this code snippet

我读过哈希表是线程安全的,因为它是同步的。考虑这个代码片段

if(!hashtable.contains(key)){
hashtable.put(key,value);
}

The operations on hashtable may not be synchronized. for example, if Thread t1access the hastable and check for key and at the same time Thread t2checks for key, before t1 executes put. now two threads are inside the if block and overwriting of key-value happens.

hashtable 上的操作可能不同步。例如,如果Thread t1访问 hastable 并检查密钥并同时Thread t2检查密钥,则在 t1 执行 put 之前。现在两个线程在 if 块中,并且会发生键值的覆盖。

so synchronized block is necessary.

所以同步块是必要的。

synchronized {
if(!hashtable.contains(key)){
    hashtable.put(key,value);
    }
}

Is this understanding correct? or is hastables safe on the operations that is performed upon hastables. I got this doubt while I was reading this post on race condition

这种理解是否正确?或者在对 hastables 执行的操作上是 hastables 安全的。我在阅读这篇关于比赛条件的文章时产生了这个疑问

采纳答案by Jeff Storey

You are correct that you need the synchronizedblock. The Hashtable'smethods are synchronized, but you still have the possibility of having a race when calling multiple methods outside of a synchronizedblock. The built-in synchronization prevents problems when two threads call putat the same time for example.

你是正确的,你需要synchronized块。该Hashtable's方法是synchronized,但你仍然有一个外部调用多种方法时有比赛的可能性synchronized块。例如,当两个线程同时调用时,内置同步可以防止出现问题put

You might also want to look into ConcurrentHashMap

您可能还想查看 ConcurrentHashMap

回答by Ted Hopp

Hashtablemethods are synchronized, but that only provides method-level protection against race conditions. (So a Hashtable—unlike a HashMap—will not become internally corrupted if multiple threads are concurrently trying to modify the data.) It is only in this sense that Hashtableis thread-safe.

Hashtable方法是同步的,但这仅提供了针对竞争条件的方法级保护。(因此,如果多个线程同时尝试修改数据,Hashtablea HashMap—与 a 不同— 不会在内部损坏。)只有在这种意义上Hashtable才是线​​程安全的。

Neither Hashtablenor a ConcurrentHashMapwill provide higher-level synchronization, which is usually*what you need when you are performing multi-step operations. You need an external synchronizedblock anyway, so you might as well use the lower-overhead HashMaprather than a Hashtable.

无论是Hashtable也不是ConcurrentHashMap将提供更高级别的同步,这通常是*您需要什么,当你正在执行多步操作。synchronized无论如何您都需要一个外部块,因此您最好使用较低的开销HashMap而不是Hashtable.

 *As Jeff Storey points out, ConcurrentHashMaphas a putIfAbsentmethod that does exactly what you are doing in your code. For other multi-step operations, ConcurrentHashMapmay or may not have a method that does what you need atomically.

 *正如 Jeff Storey 指出的那样,ConcurrentHashMap有一种putIfAbsent方法可以完全按照您在代码中执行的操作。对于其他多步操作,ConcurrentHashMap可能有也可能没有以原子方式执行您需要的方法。