Java 哈希映射的线程安全实现

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

Thread safe implementation for Hash Map

javathread-safetyconcurrenthashmaplocks

提问by pratnala

First, I'll describe what I want and then I'll elaborate on the possibilities I am considering. I don't know which is the best so I want some help.

首先,我将描述我想要什么,然后我将详细说明我正在考虑的可能性。我不知道哪个是最好的,所以我需要一些帮助。

I have a hash map on which I do read and write operations from a Servlet. Now, since this Servletis on Tomcat, I need the hash map to be thread safe. Basically, when it is being written to, nothing else should write to it and nothing should be able to read it as well.

我有一个哈希映射,我可以在该映射上从Servlet. 现在,由于这Servlet是在 Tomcat 上,我需要哈希映射是线程安全的。基本上,当它被写入时,没有其他东西应该写入它,也没有任何东西能够读取它。

I have seen ConcurrentHashMapbut noticed its get method is not thread-safe. Then, I have seen locks and something called synchronized.

我见过ConcurrentHashMap但注意到它的 get 方法不是线程安全的。然后,我看到了锁和一些叫做同步的东西。

I want to know which is the most reliable way to do it.

我想知道哪种方法最可靠。

采纳答案by Deactivator2

EDIT: removed false information

编辑:删除虚假信息

In any case, the synchronizedkeyword is a safe bet. It blocks any threads from accessing the object while inside a synchronizedblock.

无论如何,synchronized关键字是一个安全的赌注。它阻止任何线程在synchronized块内访问对象。

// Anything can modify map at this point, making it not thread safe
map.get(0);

as opposed to

// Nothing can modify map until the synchronized block is complete
synchronized(map) {
    map.get(0);
}

回答by Jigar Joshi

Collections.synchronizedMap(new HashMap<K, V>);

Collections.synchronizedMap(new HashMap<K, V>);

Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.

It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views:

返回由指定映射支持的同步(线程安全)映射。为了保证串行访问,对支持映射的所有访问都通过返回的映射完成是至关重要的。

在迭代任何集合视图时,用户必须在返回的地图上手动同步:

回答by Peter Lawrey

ConcurrentHashMap.get()is thread safe.

ConcurrentHashMap.get()是线程安全的。

You can make HashMapthread safe by wrapping it with Collections.synchronizedMap().

您可以HashMap通过用Collections.synchronizedMap().

回答by saurav

I would like to suggest you to go with ConcurrentHashMap, the requirement that you have mentioned above ,earlier I also had the same type of requirement for our application but we were little more focused on the performance side.

我想建议您采用ConcurrentHashMap上面提到的要求,之前我对我们的应用程序也有相同类型的要求,但我们更关注性能方面。

I ran both ConcurrentHashMapand map returned by Colecctions.synchronizedMap();, under various types of load and launching multiple threads at a time using JMeter and I monitored them using JProfiler .After all these tests we came to conclusion that that map returned by Colecctions.synchronizedMap()was not as efficient in terms of performance in comaprison to ConcurrentHashMap.

我使用 JMeter 在各种类型的负载和一次启动多个线程下运行了ConcurrentHashMap和 返回的映射Colecctions.synchronizedMap();,并使用 JProfiler 监视它们。经过所有这些测试,我们得出结论,返回的映射Colecctions.synchronizedMap()在性能方面效率不高与ConcurrentHashMap.

I have written a postalso on the same about my experience with both.

我也写了一篇关于我在这两者上的经历的帖子

Thanks

谢谢

回答by alexca

This is the point of ConcurrentHashMap class. It protects your collection, when you have more than 1 thread.

这是 ConcurrentHashMap 类的重点。当您有 1 个以上的线程时,它会保护您的收藏。