package edu.wtbu;标签:JUC,Thread,list,CopyOnWriteArrayList,测试,集合,new,public From: https://www.cnblogs.com/123456dh/p/17229764.html
import java.util.concurrent.CopyOnWriteArrayList;
//测试JUC安全类型的集合
public class Demo09 {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
for (int i = 0; i < 1000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(list.size());//1000
}
}