This is basically a continuation of this or this, or many others in that regard.
My question is probably simple, if I use ConcurrentHashMap::compute
only, in both readers and writers of some value, is that enough to ensure visibility?
I do know that compute
method:
The entire method invocation is performed atomically
Is that enough to guarantee visibility? Specifically, is that true specification/documentation wise with regards to happens-before
? To simplify my question, here is an example:
static class State {
private int age;
int getAge() {
return age;
}
State setAge(int age) {
this.age = age;
return this;
}
}
and :
// very simplified, no checks
static class Holder {
private static final ConcurrentHashMap<String, State> CHM = new ConcurrentHashMap<>();
public State read(String key) {
return CHM.compute(key, (x, y) -> y);
}
public State write(String key, int age) {
return CHM.compute(key, (x, y) -> y.setAge(y.getAge() + age));
}
}
No one has access to CHM
and can only work via Holder
.
To me the answer is obviously yes, this is safe and all readers will see the result of the latest write
method. I just can't connect the dots with the documentation of ConcurrentHashMap
, which is most probably obvious, but I seem to miss it.