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

前端 未结 6 1538

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

6条回答
  •  野性不改
    2021-02-07 05:43

    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.

提交回复
热议问题