在线程不安全类与写法这一节测试ArrayList时遇到问题,代码如下,和老师的一样,没什么区别:
public class ArrayListExample {
//请求总数
public static int clientTotal = 5000;
//同时并发执行的线程数
public static int threadLocal = 200;
public static List<Integer> list = new ArrayList<>();
public static void main(String[] args) throws InterruptedException{
//定义线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//定义信号量,允许并发的数目为200
final Semaphore semaphore = new Semaphore(threadLocal);
//定义CountDownLatch
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
final int count = i;
//将所有请求放入到线程池中执行
executorService.execute(() ->{
try {
semaphore.acquire();
update(count);
semaphore.release();
} catch (InterruptedException e) {
log.error("InterruptedException",e);
}
//线程每执行完一次add操作时,就调用countDownLatch
countDownLatch.countDown();
});
}
//保证countDownLatch的值为0,即所有的线程都执行完毕
countDownLatch.await();
//关闭线程池
executorService.shutdown();
log.info("list size:{}",list.size());
}
private static void update(int i){
list.add(i);
}
}
但是这段代码在运行的时候却偶尔会抛出异常:
当然,有时候也会运行出正确的结果来,想请教一下老师这里为什么数组下标会越界,ArrayList在扩容时还有什么讲究吗?