How to make Array List Thread Safe(Synchronised) in Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ThreadeSafeArrayList {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add(“A”);
list.add(“B”);
list.add(“C”);
list.add(“D”);
list.add(“E”);
list.add(“F”);

list= Collections.synchronizedList(list);

synchronized (list) {
for (String str:list) {
System.out.println(str);

}

}
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *