Concurrent threads adding to ArrayList at same time - what happens?

后端 未结 9 1893
星月不相逢
星月不相逢 2020-11-28 05:28

We have multiple threads calling add(obj) on an ArrayList.

My theory is that when add is called concurrently by two threads,

9条回答
  •  遥遥无期
    2020-11-28 06:27

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

    Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.

    Since there is no synchronization internally, what you theorize is not plausible.

    So, things get out of sync, with unpleasant and unpredictable results.

提交回复
热议问题