Connect and share knowledge within a single location that is structured and easy to search. Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list? 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". 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 Synchronized or not, an ArrayList can always be modified by multiple threads. Synchronization is about preventing concurrent access.
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. 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 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. 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. Java T point. 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. So you can also use this technique to make LinkedList synchronized and thread-safe in Java.
One of the better and up-to-date books on Java programming. Synchronized List and Iteration One of the main challenges of sharing ArrayList between multiple threads is how to deal with a situation where one thread is trying to access the element which is removed by other.
If you are using methods like get index or remove index method to retrieve or remove elements then it's also possible that other thread may also be removing other elements. This means you cannot call get index or remove index reliably without checking the size of the list first and then you also needs to provide extra synchronization between your call to size and remove int index.
Also the list will be serializable if the provided ArrayList is serializable. One of the better and up-to-date book on Java programming. Java Program to Synchronize ArrayList.
0コメント