V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nnegier
V2EX  ›  程序员

用 Java 的并发类也会抛 ConcurrentModificationException 吗?

  •  
  •   nnegier · 2022-10-13 23:36:01 +08:00 · 1435 次点击
    这是一个创建于 561 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用的 CopyOnWriteArraySet 和 ConcurrentHashMap 。

    目前分析用户的日志有这样的情况,但自己却怎么也复现不了,好痛苦(ಥ﹏ಥ)
    10 条回复    2022-10-15 01:14:38 +08:00
    oneisall8955
        1
    oneisall8955  
       2022-10-14 09:07:09 +08:00 via Android
    找到报错的代码,贴出伪代码看看
    linyinma
        2
    linyinma  
       2022-10-14 09:10:04 +08:00
    Map map

    for(Object item: map.keySet()) {

    map.remove(item)
    }

    保证抛 ConcurrentModificationException
    jorneyr
        3
    jorneyr  
       2022-10-14 09:14:34 +08:00
    @linyinma 只有 1 个元素的时候不会抛。哈哈,我们就遇到这个 Bug ,绝大部分时候 Map 里只有 1 个元素所以开发和测试都没测出来,上线后偶尔才有多个元素然后抛异常。
    detached
        4
    detached  
       2022-10-14 09:17:39 +08:00
    遍历的同时做删除就会有问题。
    GuuJiang
        5
    GuuJiang  
       2022-10-14 09:23:02 +08:00
    @linyinma 我刚看到标题时第一反应也是这个,但是 op 明确了是 ConcurrentHashMap 的前提下这个例子是不成立的,建议再仔细看看 stacktrace ,确认抛异常的操作目标确实是 ConcurrentHashMap ,就好像 stackoverflow 上有个类似的提问贴出来的例子就很有迷惑性,他虽然使用的是 ConcurrentHashMap ,但是 putAll 的参数不是 ConcurrentHashMap ,实际上是在遍历参数的时候抛的
    nnegier
        6
    nnegier  
    OP
       2022-10-14 11:04:51 +08:00
    @GuuJiang 它不是必现的,所以我也定位不到具体的哪行代码,通过日志只能定位到在一个类里,这个类里用并发数据类型存放所有的数据。如果 V 友们也没有思路,我只好在所有操作并发数据类型的地方加 try-catch 日志上报了~~~
    cubecube
        7
    cubecube  
       2022-10-14 12:00:15 +08:00
    不太可能呀,这两个类源码里面没有 ConcurrentModificationException 抛出的地方
    zilongzixue
        8
    zilongzixue  
       2022-10-14 14:56:24 +08:00
    foreach 删除 list 变量的时候,用迭代器就行
    zacard
        9
    zacard  
       2022-10-14 17:12:22 +08:00
    > Similarly, Iterators, Spliterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do <em>not</em> throw {@link java.util.ConcurrentModificationException ConcurrentModificationException}.

    你要是去看下 ConcurrentHashMap 的 java doc 就不会问这个问题了
    nnegier
        10
    nnegier  
    OP
       2022-10-15 01:14:38 +08:00 via Android
    @zacard 是这个吗? However, iterators are designed to be used by only one thread at a time.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2966 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:12 · PVG 21:12 · LAX 06:12 · JFK 09:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.