We have multiple threads calling add(obj)
on an ArrayList
.
My theory is that when add
is called concurrently by two threads,
Any number of things could happen. You could get both objects added correctly. You could get only one of the objects added. You could get an ArrayIndexOutOfBounds exception because the size of the underlying array was not adjusted properly. Or other things may happen. Suffice it to say that you cannot rely on any behavior occurring.
As alternatives, you could use Vector
, you could use Collections.synchronizedList
, you could use CopyOnWriteArrayList
, or you could use a separate lock. It all depends on what else you are doing and what kind of control you have over access to the collection.