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 have the opportunity to modify the list?
ArrayList is non synchronized because if ArrayList is synchronized then only one thread can work on ArrayList at a time and rest of all threads cannot perform other operations on the ArrayList until the first thread release the lock. This causes overhead and reduces performance. This applies for all collections.
ArrayList is non-synchronized collection and should not be used in concurrent environment without explicit synchronization.
Definition of unsynchronized : not operating or happening at the same time : not synchronized unsynchronized clocks unsynchronized movements.
Non-Synchronized means that two or more threads can access the methods of that particular class at any given time. StringBuilder is an example of a non-synchronized class. Generally, a non-synchronized class is not thread-safe. ( but some non-synchronized classes are thread-safe)
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With