Home  >  Q&A  >  body text

java - 多线程iterator修改ArrayList为何没有抛出ConcurrentModificationException异常?

迷茫迷茫2762 days ago521

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:43:42

    ArrayList is not inherently thread-safe. You can try it with the List returned by Collections.synchronizedList()

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:43:42

    In this example, the two cases of ArrayList and CopyOnWriteArrayList are tested respectively. ArrayList will generate fast-fail events, but CopyOnWriteArrayList will not generate fast-fail events.

    When using ArrayList, a fast-fail event will be generated and a ConcurrentModificationException exception will be thrown; the definition is as follows:

    private static List<String> list = new ArrayList<String>();
    

    When using CopyOnWriteArrayList, no fast-fail event will be generated; the definition is as follows:

    private static List<String> list = new CopyOnWriteArrayList<String>();
    

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:43:42

    If it is set to 100000000, an exception will be thrown. . .

    Use CopyOnWriteArrayList or ThreadLocal to put ArrayList in multi-threads

    reply
    0
  • Cancelreply