Java 线程安全哈希映射?

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

Thread safe Hash Map?

javamultithreadinghashmapjava.util.concurrent

提问by user381878

I am writing an application which will return a HashMap to user. User will get reference to this MAP. On the backend, I will be running some threads which will update the Map.

我正在编写一个应用程序,它将向用户返回一个 HashMap。用户将获得对此 MAP 的参考。在后端,我将运行一些线程来更新地图。

What I have done so far?

到目前为止我做了什么?



I have made all the backend threads so share a common channel to update the MAP. So at backend I am sure that concurrent write operation will not be an issue.

我已经创建了所有后端线程,因此共享一个公共通道来更新 MAP。所以在后端,我确信并发写操作不会成为问题。



Issues I am having

我遇到的问题



  1. If user tries to update the MAP and simultaneously MAP is being updated at backend --> Concurrent write operation problem.
  2. If use tries to read something from MAP and simultaneously MAP is being updated at backend --> concurrent READ and WRITE Operation problem.
  1. 如果用户尝试更新 MAP,同时 MAP 正在后端更新 --> 并发写入操作问题。
  2. 如果使用尝试从 MAP 读取某些内容,同时 MAP 正在后端更新 --> 并发 READ 和 WRITE 操作问题。

Untill now I have not face any such issue, but i m afraid that i may face in future. Please give sugesstions.

直到现在我还没有遇到任何这样的问题,但我担心我将来可能会遇到。请给出建议。

I am using ConcurrentHashMap<String, String>.

我在用 ConcurrentHashMap<String, String>.

采纳答案by krock

You are on the right track using ConcurrentHashMap. For each point:

使用ConcurrentHashMap. 对于每个点:

  1. Check out the methods putIfAbsentand replaceboth are threadsafe and combine checking current state of hashmap and updating it into one atomic operation.
  2. The getmethod is not synchronized internally but will return the most recent value for the specified key available to it (check the ConcurrentHashMap class Javadoc for discussion).
  1. 检查这些方法putIfAbsentreplace两者都是线程安全的,并且结合检查 hashmap 的当前状态并将其更新为一个原子操作。
  2. GET方法是不是内部同步,但会指定键可返回最近的值给它(检查讨论的ConcurrentHashMap类的Javadoc)。

The benefit of ConcurrentHashMapover something like Collections.synchronizedMapis the combined methods like putIfAbsentwhich provide traditional Map getand putlogic in an internally synchronized way. Use these methods and do nottry to provide your own custom synchronization over ConcurrentHashMapas it will not work. The java.util.concurrentcollections are internally synchronized and other threads will not respond to attempts at synchronizing the object (e.g. synchronize(myConcurrentHashMap){}will not block other threads).

ConcurrentHashMapover something的好处Collections.synchronizedMap是组合方法,例如以内部同步的方式putIfAbsent提供传统的 Mapgetput逻辑。使用这些方法并且不要尝试提供您自己的自定义同步,ConcurrentHashMap因为它不起作用。该java.util.concurrent集合被内部同步和其他线程不会在同步对象(例如,以努力作出响应synchronize(myConcurrentHashMap){}也不会妨碍其他线程)。

回答by miedwar

Side Note:

边注:

You might want to look into the lock free hash table implementation by Cliff Click, it's part of the Highly Scalable Javalibrary

您可能想查看 Cliff Click 的无锁哈希表实现,它是高度可扩展的 Java库的一部分

(Here's a Google Talkby Cliff Click about this lock free hash.)

(这是Cliff Click 关于此无锁哈希的Google Talk。)

回答by Moisei

ConcurrentHashMap was designed and implemented to avoid any issues with the scenarios you describe. You have nothing to worry about.

ConcurrentHashMap 的设计和实现是为了避免您描述的场景出现任何问题。你没有什么可担心的。

A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.updates.

一个哈希表,支持检索的完全并发性和可调整的 update.updates 预期并发性。

javadoc of ConcurrentHashMap

ConcurrentHashMap 的 javadoc