public static void main(String[] args) throws InterruptedException {
initThread(new AddThread(), 10);
initThread(new ReadThread(), 100);
}
private static void initThread(Runnable runnable, int maxNum) throws InterruptedException {
Thread[] ts = new Thread[maxNum];
for (int k = 0; k < maxNum; k++) {
ts[k] = new Thread(runnable);
}
for (int k = 0; k < maxNum; k++) {
ts[k].start();
}
}
}
下图运行结果中可以看出来, 同一个线程, 即使在读的过程中发生了size变化, 也不会抛出ConcurrentModificationException