hbf548

多看源码多读书

0%

JUC 安全测试(扩充)

CopyOnWriteArrayList本身就是安全的!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//测试JUC安全类型的集合
public class TestJuc {
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());
}
}

![image.png](JUC 安全测试(扩充)/1627131835629-4d95fb2c-d883-48ef-880e-987cce6d7399.png)