group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Archana Priyadarshini
Archana Priyadarshini
Computer Science
08 Feb 2019

Synchronization of ArrayList in java

ArrayList is non-synchronized collection and should not be used in concurrent environment without explicit synchronization. It means if a thread modifies it structurally and multiple threads access it concurrently, it must be synchronized externally. Structural modification means addition or deletion of element(s) from the list or explicitly resizes the backing array. Changing the value of existing element is not structural modification.

There are two way to create Synchronized Arraylist.

1. Collections.synchronizedList() method. This method returns a synchronized thread-safe list backed by the specified list. It is recommended that we should manually synchronize on the returned list when traversing it via Iterator, Spliterator or Stream. Else it may result in non-deterministic behavior.

2. Using CopyOnWriteArrayList. CopyOnWriteArrayList is a thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array