manikanta nvsr
manikanta nvsr

Reputation: 567

Why are we here, specifically, saying that ArrayList is not thread safe?

Description: If we use same object reference among multiple threads, no object is thread safe. Similarly, if any collection reference is shared among multiple threads then that collection is not thread-safe since other threads can access it. So, Why are we here specifically saying that ArrayList is not thread-safe? What about the other Collections?

Upvotes: 0

Views: 1216

Answers (3)

Solomon Slow
Solomon Slow

Reputation: 27115

You misunderstand the meaning of "thread-safe."

When we say "class X is thread-safe," We are not saying that you don't have to worry about the thread-safety of a program that uses it. If you build a program using nothing but thread-safe objects, that does not guarantee that your program will be thread-safe.

So what does it guarantee?

Suppose you have a List. Suppose that two threads, A and B, each write different values to the same index in the list, suppose that some thread C reads from that index, and suppose that none of those three threads uses any synchronization.

If the list is "thread-safe," then you can be assured that thread C will get one of three possible values:

  • The value that thread A wrote,
  • The value that thread B wrote,
  • The value that was stored at that index before either thread A or thread B wrote.

If the list is not thread-safe, then any of those same three things could happen, but also, other things could happen:

  • Thread C could get a value that was never in the list,
  • The list could behave in broken ways in the future for thread C even if no other thread continues to use it,
  • The program could crash,
  • etc. (I don't know how many other strange things could happen.)

When we say that a class is "thread-safe" we are saying that it will always behave in predictable, reasonable ways, even when its methods are concurrently called by multiple threads.

If you write a program that uses a "thread-safe" list, and if it depends on thread C reading one particular value of the three possibilities that I listed above, then your program has a thread-safety problem, even though the list itself does not.

Upvotes: 3

Hirad Nikoo
Hirad Nikoo

Reputation: 1629

ArrayList is unsynchronized in implementation. When an object is unsynchronized it means that is is not locked while being modified structurally. A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification. What you are referring to is an array which the elements are being added to or being deleted from and can be modified this differs from it having its value being set.

Reference is in regards with the pointer of the start of the array but how many elements are there is in question and having an unsynchronized object being modified in the sense of elements while the elements are being iterated over by another thread the integrity of the elements in the list is hard to guarantee. I hope I was able to convey the message plainly.

Look for more details here in Oracle: Array List and ConcurrentModificationException

ArrayList:

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method.

ConcurrentModificationException:

Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.

Upvotes: 0

Tobias Wichtrey
Tobias Wichtrey

Reputation: 1218

I haven't checked but I think that all standard Collection implementations state if they are thread-safe or not. So you know if you can share that collection among different threads without synchronization.

CopyOnWriteArrayList for example is a thread-safe List implementation.

Upvotes: 1

Related Questions