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>();
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:
When using CopyOnWriteArrayList, no fast-fail event will be generated; the definition is as follows:
If it is set to 100000000, an exception will be thrown. . .
Use CopyOnWriteArrayList or ThreadLocal to put ArrayList in multi-threads