加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Collections.synchronizedMap(new LinkedHashMap());没

发布时间:2020-12-15 00:02:42 所属栏目:Java 来源:网络整理
导读:我正在使用以下构造来创建线程安全Map. Collections.synchronizedMap(new LinkedHashMap()); 虽然我收到ConcurrentModificationException错误. 解决方法 没有代码,很难猜出什么是真正的问题,但我的猜测是,你没有使用返回的集合来执行操作.按照 javadoc In or
我正在使用以下构造来创建线程安全Map.
Collections.synchronizedMap(new LinkedHashMap());

虽然我收到ConcurrentModificationException错误.

解决方法

没有代码,很难猜出什么是真正的问题,但我的猜测是,你没有使用返回的集合来执行操作.按照 javadoc

In order to guarantee serial access,it is critical that all access to the backing collection is accomplished through the returned collection.
It is imperative that the user manually synchronize on the returned collection when iterating over it:

Collection c = Collections.synchronizedCollection(myCollection);
     ...
  synchronized(c) {
      Iterator i = c.iterator(); // Must be in the synchronized block
      while (i.hasNext())
         foo(i.next());
  }

不遵循此建议可能会导致非确定性行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读