I'm modifying same list from multiple threads, shouldn't it trigger ConcurrentModificationException while iterating list?
What could be done to trigger this exception?
public class ConcurrentTest {
static List<String> list = setupList();
public static List<String> setupList() {
System.out.println("setup predefined list");
List<String> l = new ArrayList();
for(int i = 0; i < 50;i++) {
l.add("test" + i);
}
return l;
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(50);
for(int i = 0; i < 50; i++) {
executorService.submit( () -> {
list.add("key1");
Thread currentThread = Thread.currentThread();
System.out.println( Thread.currentThread().getName() + ", " + list.size() );
for(String val: list) {
try {
Thread.sleep(25);
}
catch(Exception e) {
}
}
});
}
executorService.shutdown();
}
}