Shouldn't that crash be impossible?
It is not impossible if there is another thread modifying the ArrayList
.
For example:
if (
(savedState.userEntries.size()>0) &&
// Another thread empties the list NOW
(savedState.userEntries.get(savedState.userEntries.size()-1).getDuration()==0)
) {
If these data structures could possibly be accessed and modified from more than one thread, then either:
- all operations it should use
synchronized
or some other locking to ensure thread-safety, or
- the array list should be replaced with a concurrent data structure.
You need to choose between the approaches based on an overall assessment of how the current data structure is used. For example, you may want to replace it with a Queue
.
(Some potential solutions won't work; e.g. replacing ArrayList
with synchronizedList(list)
won't work here because you are doing 2 list operations here (size
& get
) that need to be performed as an atomic operation.)
You express doubts:
I don't know. It can change theoretically but if it happened to two users in two days and within the time between those evaluations, it just seems extremely unlikely.
Extremely unlikely events do happen.
Your intuition about "extremely unlikely" is probably wrong (based on the evidence!). Unless you do a thorough mathematical analysis (or lots of measurements) you can't actually quantify the probability of "extremely unlikely".
The probabilities can be affected by how you softeare is used, differences / changes in platform behavior ans so on.
Do you have an alternative explanation supported by evidence?
An experienced programmer (in concurrency) would recognize this as a potential cause, and fix it anyway, irrespective of his / her gut feeling of likelihood.