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();
tmp = new String("x") --hb--> x.put(1, tmp) --hb--> x.get(1) --hb--> read(p.x)But in other cases where non-final fields are involved, e.g. if p.x was a Foo with a non-final field y with a getter, p.x.getY() may return an uninitialized value after the get() returns a non-null p.
class Foo {
public Foo(int i) { x = i;}
public int x;
}
ConcurrentHashMap<Integer, Foo> x = new ConcurrentHashMap<>();
new Thread(() -> { // Thread 1
x.put(1, new Foo(1)); // 1
}).start();
new Thread(() -> { // Thread 2
Foo p = x.get(1); // 2
if(p != null){ // 3
print(p.x); // 4
}
}).start();
More formally, an update operation for a given key bears aVladimir
* <em>happens-before</em> relation with any (non-null) retrieval for
* that key reporting the updated value.
Retrievals reflect the results of the most recently completed update operations holding upon their onset. (More formally, an update operation for a given key bears a happens-before relation with any (non-null) retrieval for that key reporting the updated value.)
class C {
..
mutable fields
..
final boolean publishSafely;
}
In this case that contract is provided by JLS "17.4.3. Programs and Program Order" and "17.4.5. Happens-before Order" .TL;DR: "If x and y are actions of the same thread and x comes before y in program order, then hb(x, y)."
--Vladimir
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 mechanical-sympathy+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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 mechanical-symp...@googlegroups.com.
--
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 mechanical-symp...@googlegroups.com.
Thanks Vladimir for your thoroughly explanation, I need to re read the Aleksey's JMM pragmatics 10 times more I guess 🙄
On Sun, Nov 18, 2018 at 7:35 PM Vladimir Sitnikov <sitnikov...@gmail.com> wrote:
Jean-Philippe>is a write to value but no read of this va lue inside the same thread, so the write is free to be reordered
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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 mechanical-sympathy+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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 mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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 mechanical-symp...@googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "mechanical-sympathy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mechanical-sympathy/lxlJBA49EoM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mechanical-symp...@googlegroups.com.
By the way, how to put an element safely (fully constructed) to ConcurrentHashMap if we need mutable object? I see only one solution (by adding a "mock" final field):