DEV Community

aryan
aryan

Posted on

Synchronized ArrayList in Java

Implementation of arrayList is not synchronized is by default. It means if a thread modifies it structurally and multiple threads access it concurrently, it must be synchronized externally. Structural modification means the addition or deletion of the element from the list or explicitly resizes the backing array. Changing the value of existing element is not structural modification.

See Reference: https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html

import java.util.*;

class ArrayListExample
{
public static void main (String[] args)
{
List list =
Collections.synchronizedList(new ArrayList());

    list.add("A"); 
    list.add("B"); 
    list.add("C"); 

    synchronized(list) 
    { 
        // must be in synchronized block 
        Iterator it = list.iterator(); 

        while (it.hasNext()) 
            System.out.println(it.next()); 
    } 
} 
Enter fullscreen mode Exit fullscreen mode

}

See Reference: https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html

Further Readings
https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html
https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html
https://www.java8net.com/2020/03/initialize-arraylist-in-java.html
https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html
https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay