Создание ConcurrentModificationExecption с помощью Java HashSetJAVA

Программисты JAVA общаются здесь
Anonymous
Создание ConcurrentModificationExecption с помощью Java HashSet

Сообщение Anonymous »

Я просто тестирую несколько вещей, чтобы улучшить свое понимание, и написал следующий код для создания ConcurrentModificationExecption

Код: Выделить всё

public static void main(String[] args) throws InterruptedException {
Set 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();
}
Однако исключение не возникает, как я ожидал. Когда я заменяю HashSet на и ArrayList, я, например, получаю исключение.
Есть идеи, почему?

Подробнее здесь: https://stackoverflow.com/questions/788 ... va-hashset

Вернуться в «JAVA»