I need a scenario to generate ConcurrentModificationException using multi-thread. Please have a look at the below code, here I am getting IllegalThreadStateException.
Not sure why this is happening and also what could be the correct example for my scenario.
public class MultiThreadFailMain {
public static void main(String[] args) {
List<Integer> l = new CopyOnWriteArrayList<>(Arrays.asList(1,2,3,4));
Thread t1 = new Thread(() -> {
l.add(1);
});
Thread t2 = new Thread(() -> {
l.add(1);
});
l.forEach(v -> {
t1.start();
t2.start();
});
System.out.println("n---------------");
l.forEach(System.out::print);
}
}