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

你。 提交于 2021-02-06 09:52:05

问题


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?


回答1:


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.




回答2:


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) {
         ...
      }
}



回答3:


It means that instances of ArrayList are not guaranteed to be threadsafe. This usually includes both read and write access. If you do it without external synchronization you can leave the object in stange states and get some hard to debug behavior.




回答4:


Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list ?

Yes. If multiple threads operate on it at the same time, it may result in unexpected behaviour




回答5:


Being synchronized means that every operation is thread safe - if you use the same Array List from two threads at the same time, they can't corrupt the state. However, this makes it slower.

By default ArrayList is not synchronized, you can achieved that by synchronized keyword

ArrayList al=new ArrayList();

Collections.synchronized(al);



回答6:


Being synchronized means that every operation is thread safe - if you use the same vector from two threads at the same time, they can't corrupt the state. However, this makes it slower.

If you are working in a single threaded environment (or the list is limited to a thread and never shared), use ArrayList. If you are working with multiple threads that share the same collection, either use Vector, or use ArrayList but synchronize in some other way (e.g., manually or via a wrapper).



来源:https://stackoverflow.com/questions/6910512/what-does-it-mean-when-we-say-an-arraylist-is-not-synchronized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!