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
hashtable and synchronization in Java
提问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 t1
access the hastable and check for key and at the same time Thread t2
checks 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 synchronized
block. The Hashtable's
methods are synchronized
, but you still have the possibility of having a race when calling multiple methods outside of a synchronized
block. The built-in synchronization prevents problems when two threads call put
at the same time for example.
你是正确的,你需要synchronized
块。该Hashtable's
方法是synchronized
,但你仍然有一个外部调用多种方法时有比赛的可能性synchronized
块。例如,当两个线程同时调用时,内置同步可以防止出现问题put
。
You might also want to look into ConcurrentHashMap
您可能还想查看 ConcurrentHashMap
回答by Ted Hopp
Hashtable
methods 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 Hashtable
is thread-safe.
Hashtable
方法是同步的,但这仅提供了针对竞争条件的方法级保护。(因此,如果多个线程同时尝试修改数据,Hashtable
a HashMap
—与 a 不同— 不会在内部损坏。)只有在这种意义上Hashtable
才是线程安全的。
Neither Hashtable
nor a ConcurrentHashMap
will provide higher-level synchronization, which is usually*what you need when you are performing multi-step operations. You need an external synchronized
block anyway, so you might as well use the lower-overhead HashMap
rather than a Hashtable
.
无论是Hashtable
也不是ConcurrentHashMap
将提供更高级别的同步,这通常是*您需要什么,当你正在执行多步操作。synchronized
无论如何您都需要一个外部块,因此您最好使用较低的开销HashMap
而不是Hashtable
.
*As Jeff Storey points out, ConcurrentHashMap
has a putIfAbsent
method that does exactly what you are doing in your code. For other multi-step operations, ConcurrentHashMap
may or may not have a method that does what you need atomically.
*正如 Jeff Storey 指出的那样,ConcurrentHashMap
有一种putIfAbsent
方法可以完全按照您在代码中执行的操作。对于其他多步操作,ConcurrentHashMap
可能有也可能没有以原子方式执行您需要的方法。