What does it mean when we say an ArrayList is not synchronized?
Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects hav
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) {
...
}
}