So, first, the documentation for ArrayList says that it is not thread safe, so I would not recommend using it in such a case. Moreover, even if it worked theoretically, you would have the problem of ensuring that someone who supports this program remembers the strict restrictions that you imposed on reading and writing with fun if they did not! However, your question is really interesting from a theoretical point of view, so I thought I'd take a look.
The first thing to note is that any of the readers tried to access the list through an iterator, and sooner or later the reading will fail with a ConcurrentModificationException. This is because every modification of the list is tracked, and the Iterator does not work quickly if it detects the modification outside the iterator.
However, you do not indicate the need for using iterators in the original question, and what happens if you restrict yourself to just adding and reading with get () and size ()? In this case, I still think that you can see a random failure, because inside the list stores the current size along with the backup data array, and not a single variable is marked as mutable. As a result (and because of the guarantees of Java memory), I think it is theoretically possible for an updated size variable to appear in one of the reader threads, while the data is still not updated. In this case, you can return the returned spam data or even indexOutOfBounds.
source share