简体   繁体   English

如果从多个线程中使用,则HashMap#put / remove是否会遇到任何ConcurrentModificationException

[英]Will HashMap#put/remove suffer any ConcurrentModificationException if used from multiple threads

In Test.java 在Test.java中

public static Map<String, Integer> testMap=new HashMap<String, Integer>();

In TestServlet.java 在TestServlet.java中

public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
  Test.testMap.put("cool", 1);
}

In TestServlet2.java 在TestServlet2.java中

public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
  Test.testMap.remove("cool");
}

In test.html , there is a function to call TestServlet every 3 secs test.html ,有一个函数每3秒调用一次TestServlet。

mTimer = setTimeout('getTest();',3000); //call TestServlet in 3 seconds

In test2.html , there is a function to call TestServlet2 every 2 secs test2.html ,有一个函数每2秒调用一次TestServlet2。

mTimer = setTimeout('getTest2();',2000); //call TestServlet in 2 seconds

Now, I run test.html & test2.html at the same time & no problem occur. 现在,我同时运行test.htmltest2.html ,不会出现任何问题。

Of course, if I replace public static Map<String, Integer> testMap=new HashMap<String, Integer>(); 当然,如果我替换public static Map<String, Integer> testMap=new HashMap<String, Integer>(); with public static List<String> testList=new ArrayList<String>(); 使用public static List<String> testList=new ArrayList<String>(); , then I got ConcurrentModificationException ,然后我得到了ConcurrentModificationException

My question is that? 我的问题是? Can HashMap suffer any ConcurrentModificationException or be Thread-safe? HashMap可以遭受任何ConcurrentModificationException还是线程安全的?

I have not tried to modify the value of each key of the Map so I don't know what is going on. 我没有尝试修改Map每个键的值,所以我不知道发生了什么。

HashMap s are not thread safe and should be used wisely. HashMap不是线程安全的,应该明智地使用。 If you want to use key value pairs type of data structure ConcurrentHashMap is recommended in multithreaded environment. 如果要使用键值对,则在多线程环境中建议使用ConcurrentHashMap类型的数据结构。

Yes - you can see from the source code that the iterator throws such an exception: 是的-您可以从源代码中看到迭代器抛出此类异常:

808         final Entry<K,V> nextEntry() {
809             if (modCount != expectedModCount)
810                 throw new ConcurrentModificationException();

And modCount is incremented in the put method and other places. 并且modCountput方法和其他位置增加。

Note that this is not necessarily restricted to the issue of thread-safety. 注意,这不一定限于线程安全问题。 A same-thread sequence of operations could cause the exception to be thrown if a put operation was done while between two iterator operations. 如果在两个迭代器操作之间执行put操作,则同一线程的操作序列可能导致引发异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在并发线程中操作`values()`和`put()`时避免使用HashMap“ConcurrentModificationException”? - How to avoid HashMap “ConcurrentModificationException” while manipulating `values()` and `put()` in concurrent threads? 如何在没有ConcurrentModificationException的情况下删除哈希图中的多个映射? - How to remove multiple mappings in hashmap without ConcurrentModificationException? 可以在没有迭代的情况下在 HashMap 中获取、放置和删除元素导致 ConcurrentModificationException? - Can get, put & remove element in HashMap without iteration cause ConcurrentModificationException? 为什么代码会挂起来自多个线程的HashMap.put()? - Why does the code hang with HashMap.put() from multiple threads? 为什么 ArrayList 从多线程修改时不抛出 ConcurrentModificationException? - Why does ArrayList not throw ConcurrentModificationException when modified from multiple threads? 从多个线程访问Hashmap而不进行同步 - Accessing Hashmap from multiple threads without synchronization HashMap中的ConcurrentModificationException - ConcurrentModificationException in HashMap Android - ConcurrentModificationException - 在不同的线程中读取和删除 - Android - ConcurrentModificationException - Read and remove in different threads 知道为什么我在从 HashMap 中删除键时没有收到 java.util.ConcurrentModificationException 吗? - Any Idea why I am not getting java.util.ConcurrentModificationException while removing key from HashMap? 从 HashMap 中删除项目会产生 ConcurrentModificationException - Removing items from HashMap gives ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM