I am trying to test concurrent puts to a ConcurrentHashMap
using multiple threads, but the size of the map after processing is not what I expected.
I have the following code to insert 1000 entries into a ConcurrentHashMap
:
@Test
public void testThreadSafetyConcurrentHashMap() {
Map<Integer, Integer> map = new ConcurrentHashMap<>();
Runnable runnable = () -> {
for (int i = 0; i < 1000; i++) {
map.put(i, i);
}
};
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (int i = 0; i < 4; i++) {
executorService.submit(runnable);
}
System.out.println(map.size());
}
I am expecting 1000 items for the map.size()
call, but I don't get this every time.
Can someone tell me what the problem is? I thought 4 threads putting 1000 items at the same time, would result in a total of 1000 items at the end?
Thread.sleep(1000);
before printing and you'll see.