What does it mean when we say an ArrayList is not synchronized?
What does it mean when we say an ArrayList is not synchronized?
It means that accessing an ArrayList
instance from multiple threads may not be safe (read, "may result in unexpected behavior" or "may not work as advertised").
Further reading:
- Synchronization and thread safety in Java
- Meaning of Java thread safety.
Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list?
Even if it would have been thread safe, multiple threads would be able to modify the list.
The difference is that if it's not thread safe and multiple threads access the list, all bets are off. Saying that the class is not thread safe, is the same as adding "If accessed from one thread at a time, this method works as follows....." in front of every method description.
Synchronized or not, an ArrayList can always be modified by multiple threads. Synchronization is about preventing concurrent access.
With ArrayList (or Collections in general) there are two concurrency problems.
First, there is method synchronization. This means, all calls to methods of an ArrayList instance are synchronized. So there is always only one method executed at a time. All other method calls that happen while the first method still computes are queued until the running method is completed.
Method synchronization can be ensured by wrapping an ArrayList like this:
List list = Collections.synchronizedList(new ArrayList());
Example: assume two threads try to do the following at the same time:
list.add(0, "test");
If you have a synchronized list, you are guaranteed that the list afterwords starts with two "test" entries. If the list is not synchronized, you might get a list with only one "test" entry... or other unexpected results.
Second, there is instance synchronization. Here we not only prevent concurrent method calls, but we make sure that only one thread has access to the list object for a time. This is important if you have pieces of logic that require that the list remains in an unchanged state until the logic is done. For example iterating over lists. You don't want other threads to add elements while you iterate over a list.
This kind of synchronization is done by wrapping your piece of logic with a synchronized block:
synchronized(list) {
for (Object o:list) {
...
}
}