I am just testing a few things out to improve my understanding and I wrote the following code to produce a ConcurrentModificationExecption
public static void main(String[] args) throws InterruptedException {
Set<String> set = new HashSet<>();
Runnable updateList = () -> {
while (true) {
set.add("Hello");
}
};
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
executorService1.execute(updateList);
Runnable printList = () -> {
while (true) {
if (set.contains("Hello")) {
System.out.println(set);
set.add("Bye");
}
}
};
ExecutorService executorService2 = Executors.newSingleThreadExecutor();
executorService2.execute(printList);
Thread.sleep(1000);
executorService1.shutdown();
executorService2.shutdown();
}
However its not producing the exception like I thought it would. When I replace the HashSet with and ArrayList, I egt the exception.
Any idea why?