I tried a few times with different code, it seems that the run() of producer somehow doesn't execute to the end. The print was never shown nor map updated. So in the run() of consumer, the map.isEmpty() always true.
I can't figure out why. They have different threads, but the thread for Producer's run() only executes half way?
package com.codegym.task.task26.task2611;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/*
The world doesn't change, we change
*/
public class Solution {
public static void main(String[] args) throws Exception {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
Producer producer = new Producer(map);
Consumer consumer = new Consumer(map);
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(producer);
executorService.submit(consumer);
Thread.sleep(2000);
executorService.shutdownNow();
// Finally, 5 lines have to be printed
}
}