How to synchronize an ArrayList in Java?

Java 8Object Oriented ProgrammingProgramming

The synchronizedList(List<T> list) method of the Collections class accepts a List object and returns a synchronized list backed by the specified list.

Example:

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class ArrayListSample {
   public static void main(String[] args){
      ArrayList<String> list = new ArrayList<String>();
      list.add("JavaFx");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      Set<String> set = new LinkedHashSet<String>(list);
      Collections.synchronizedList(list);
      System.out.println(list);
   }
}

Output:

[JavaFx, Java, WebGL, OpenCV]
raja
Published on 17-Jan-2018 10:32:48
Advertisements