Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when we say an ArrayList is not synchronized?

Tags:

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?

like image 666
Oh Chin Boon Avatar asked Aug 02 '11 10:08

Oh Chin Boon


People also ask

What does it mean that ArrayList is not synchronized?

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.

Is ArrayList synchronized or not?

ArrayList is non-synchronized collection and should not be used in concurrent environment without explicit synchronization.

What is the meaning of non synchronized?

Definition of unsynchronized : not operating or happening at the same time : not synchronized unsynchronized clocks unsynchronized movements.

What is the meaning of non synchronized in Java?

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)


1 Answers

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.

like image 122
aioobe Avatar answered Oct 22 '22 18:10

aioobe