java - 多线程iterator修改ArrayList为何没有抛出ConcurrentModificationException异常?
迷茫
迷茫 2017-04-18 10:41:42
0
3
496
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
左手右手慢动作

ArrayList is not inherently thread-safe. You can try it with the List returned by Collections.synchronizedList()

迷茫

In this example, the two cases of ArrayList and CopyOnWriteArrayList are tested respectively. ArrayList will generate fast-fail events, but CopyOnWriteArrayList will not generate fast-fail events.

When using ArrayList, a fast-fail event will be generated and a ConcurrentModificationException exception will be thrown; the definition is as follows:

private static List<String> list = new ArrayList<String>();

When using CopyOnWriteArrayList, no fast-fail event will be generated; the definition is as follows:

private static List<String> list = new CopyOnWriteArrayList<String>();
迷茫

If it is set to 100000000, an exception will be thrown. . .

Use CopyOnWriteArrayList or ThreadLocal to put ArrayList in multi-threads

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!