How to make a List Thread Safe using Collections class in java.


How to make Array List as a Thread safe


Demo.java


package com.deepsingh44.mail;

import java.util.ArrayList;
import java.util.Collections;

public class Demo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("deepsingh44.blogspot.com");
list.add("deepsingh44");
list.add("github.com/deepsingh44");
Collections.synchronizedList(list);
for(String item :list) {
System.out.println(item);
}
}
}



Output :

deepsingh44.blogspot.com
deepsingh44
github.com/deepsingh44

No comments: