测试JUC安全类型的集合:CopyOnWriteArrayList
代码:
package multithreading.TestSynchronized;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.concurrent.CopyOnWriteArrayList;
// 测试JUC安全类型的集合
public class TestJUC {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
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());
}
}
运行结果:
疑惑点:当线程休眠时间过短时,还是会出现集合对象覆盖的情况