package pc;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
public class D {
public static void main(String[] args) {
// List<String> list = new ArrayList<>(); //报错
// List<String> list = new Vector<>(); //√速度慢
// List<String> list = Collections.synchronizedList(new ArrayList<>());//√速度慢
List<String> list = new CopyOnWriteArrayList<>();//√高效
for (int i = 1; i <=10; i++) {
new Thread(()->{
list.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
List 不安全
最新推荐文章于 2024-05-28 18:00:00 发布