Search code examples
javaarraylistsynchronized

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


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?


Solution

  • 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.