Hello, let's look at:
class P { public String x;
}
ConcurrentHashMap<Integer, P> x = new ConcurrentHashMap<>();
new Thread(() -> { // Thread 1
x.put(1, new String("x")); // 1
}).start();
new Thread(() -> { // Thread 2
P p = x.get(1); // 2
if(p != null){
print(p.x); // 4
}
}).start();
If thread 2 observes p != null is it guaranteed by JMM that p.x is
initialized? For my eye, yes, because:
Let's assume a such execution when p != null was true. It means that there
is a synchronize-with relation between x.put(1, new String("x")); --sw-->
x.get().
Putting and getting an element from ConcurrentHashMap contain
synchronization access (and, actually, synchronization-with is between
them).
In a result, there is a chain of happens-before relation:
tmp = new String("x") --hb--> x.put(1, tmp) --hb--> x.get(1) --hb-->
read(p.x)
Yes?
--
You received this message because you are subscribed to the Google Groups
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.