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

后端 未结 9 1922
星月不相逢
星月不相逢 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 06:33

    You could use instead of ArrayList(); :

    Collections.synchronizedList( new ArrayList() );
    

    or

    new Vector();
    

    synchronizedList as of me preferable because it's:

    • faster on 50-100%
    • can work with already existing ArrayList's

提交回复
热议问题