How to deal with ConcurrentModificationException

后端 未结 3 644
深忆病人
深忆病人 2020-12-12 03:26

I am getting the a ConcurrentModificationException from my cooldown timer. I use a thread to reduce the values every second like this:

public class CoolDownT         


        
3条回答
  •  隐瞒了意图╮
    2020-12-12 04:25

    A ConcurrentModificationException is thrown while you try to modify the contents of your Collection, at the same time while Iterating through it.

    Read this and this for more discussion on it.

    The reason why sometimes it might work out for you is clearly mentioned in the documentation.

    The iterators returned by all of this class's "collection view methods" are fail-fast: if 
    the map is structurally modified at any time after the iterator is created, in any way 
    except through the iterator's own remove method, the iterator will throw a 
    ConcurrentModificationException. Thus, in the face of concurrent modification, the 
    iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic 
    behavior at an undetermined time in the future.
    
    Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally 
    speaking, impossible to make any hard guarantees in the presence of unsynchronized 
    concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a 
    best-effort basis.
    

提交回复
热议问题