Suppose that i have a shared ConcurrentHashMap<String, Integer> called map that has already only one mapping ("One", 1), and suppose also that i have 2 threads.
The first thread executes this code:
map.put("One", 2);
and the second thread executes this code:
synchronized (map) {
Integer number = map.get("One");
System.out.println(number == map.get("One"));
}
Since ConcurrentHashMap works with lock striping method instead of locking entire object i don't think that the described scenario is thread safe.
Particularly i don't know if there could be an interleaving of map.put("One", 2);
in first thread between Integer number = map.get("One");
call and System.out.println(number == map.get("One"));
call in second thread despite both are inside a synchronized block.
So is it possible that that code prints false?