CopyOnWriteArrayList JUC当中安全容器
package 多线程练习; import java.util.concurrent.CopyOnWriteArrayList; /* CopyOnWriteArrayList 是JUC当中的安全容器其中用了lock锁 可重入锁 */ public class CopyOnWriteArrayListDemo { public static void main(String[] args) { CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>(); for (int i = 0; i < 10000; i++) { new Thread(() -> { list.add(Thread.currentThread().getName()); }).start(); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(list.size()); } }
输出结果