We have multiple threads calling add(obj)
on an ArrayList
.
My theory is that when add
is called concurrently by two threads,
You could also get a null
, an ArrayOutOfBoundsException
, or something left up to the implementation. HashMap
s 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.
java.util.concurrent has a thread-safe array list. The standard ArrayList is not thread-safe and the behavior when multiple threads update at the same time is undefined. There can also be odd behaviors with multiple readers when one or more threads is writing at the same time.
You could use instead of ArrayList();
:
Collections.synchronizedList( new ArrayList() );
or
new Vector();
synchronizedList
as of me preferable because it's: