Код: Выделить всё
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();
}
Есть идеи, почему?
Подробнее здесь: https://stackoverflow.com/questions/788 ... va-hashset