Is modCount an atomic variable?

I read that a ConcurrentModificationException can be thrown whenever a thread performs a structural modification upon a certain list while another thread is iterating over its elements. To detect such modification, instances of the class List store the number of times they were modified in a field called modCount, whose value is checked at each… Read More Is modCount an atomic variable?

Can using foreach of CopyOnWriteArrayList cause ConcurrentModificationException in java?

I look to java 11 implementation of .foreach method in CopyOnWriteArrayList public void forEach(Consumer<? super E> action) { Objects.requireNonNull(action); for (Object x : getArray()) { @SuppressWarnings("unchecked") E e = (E) x; action.accept(e); } } I see that it just loops the array without any locks. Can add() or remove() performed concurrently with foreach give a… Read More Can using foreach of CopyOnWriteArrayList cause ConcurrentModificationException in java?

Iterating through collections to add items but throwing ConcurrentModificationException

package com.ripal; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class Outputs { public void show() { final ArrayList<String> list = new ArrayList<String>(); list.add("banana"); list.add("apple"); Iterator<String> itr = list.iterator(); Collections.sort(list); while (itr.hasNext()) { System.out.println(itr.next() + " "); } } } class Test { public static void main(String[] args) { Outputs outputs = new Outputs(); outputs.show(); }… Read More Iterating through collections to add items but throwing ConcurrentModificationException