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

后端 未结 9 1932
星月不相逢
星月不相逢 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:29

    You could also get a null, an ArrayOutOfBoundsException, or something left up to the implementation. HashMaps have been observed to go into an infinite loop in production systems. You don't really need to know what might go wrong, just don't do it.

    You could use Vector, but it tends to work out the interface is not rich enough. You will probably find that you want a different data structure in most cases.

提交回复
热议问题