Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading ArrayList in Concurrent system

I have a simple ArrayList and I am feeding this ArrayList from multiple Threads via Java concurrency. Each Thread will only read the same instance of this ArrayList. Is there any chance of error during the reading operation?

like image 774
Tapas Bose Avatar asked Nov 09 '12 14:11

Tapas Bose


People also ask

Is ArrayList concurrent?

Implementation of ArrayList is not synchronized by default. It means if a thread modifies it structurally and multiple threads access it concurrently, it must be synchronized externally.

Is ArrayList multithreaded?

We know that by default ArrayList class is not a thread-safe or non-synchronized. That means the multiple threads can access the same ArrayList object or instance simultaneously.

Why ArrayList is non synchronized?

ArrayList is non synchronized because if ArrayList is synchronized then only one thread can work on ArrayList at a time and rest of all threads cannot perform other operations on the ArrayList until the first thread release the lock. This causes overhead and reduces performance.

Is ArrayList get thread-safe?

ArrayList , on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList .


1 Answers

Provided there are no more writes make it immutable using Collections.unmodifiableList and then forget about read issues.

like image 194
Ajay George Avatar answered Sep 20 '22 23:09

Ajay George