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

7

23

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?

Sobersided answered 2/8, 2011 at 10:20 Comment(0)
R
17

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.

Rojas answered 2/8, 2011 at 10:21 Comment(0)
C
7

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) {
         ...
      }
}
Confiscate answered 2/8, 2011 at 11:4 Comment(1)
If we use same object reference among multiple threads, no object is thread safe. why are we here specifically saying that ArrayList is not thread safe?Irreversible
T
2

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.

Trehalose answered 2/8, 2011 at 10:22 Comment(0)
T
1

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

Tintinnabulation answered 2/8, 2011 at 10:21 Comment(0)
F
0

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);
Fetal answered 2/8, 2011 at 10:32 Comment(0)
T
0

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

Thornburg answered 14/5, 2019 at 5:54 Comment(0)
R
0

Accessing arraylist's instances from multiple threads is not safe considerable. There are some alternate way to go there **Arraylist ** is not like linkdlist. They may sound similar but not made internally same.

Arraylist<datamodel> are;
Resnick answered 16/11, 2022 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.