admin管理员组文章数量:1357268
If in my class I have a field of type ImmutableMap
like:
private ImmutableMap<String, State> states = ImmutableMap.of();
And there is 1 reader thread and 1 writer thread
Reader reads by calling methods like get
on states
with no synchronized
or any kinds of barriers.
Writer thread writes by reassigning states
to new variables like states = ImmutableMap.of(...)
in a synchronized
method (which, as far as I know, provides no guarantee of visibility in the other threads if they have no read memory barriers).
So the whole thing looks like this:
State read(String key) {
return states.get(key);
}
synchronized void write(String key, State value) {
var hashMap = new HashMap<String, State>();
states.forEach(hashMap::put);
hashMap.put(key, value);
states = ImmutableMap.of(hashMap);
}
Is it true, that reader, when reading from a field states
gets a valid value of states
, that existed at one point of the time (linearizability is present)? Can reader thread read some nonsense when reading value of states
due to compiler reordering?
If in my class I have a field of type ImmutableMap
like:
private ImmutableMap<String, State> states = ImmutableMap.of();
And there is 1 reader thread and 1 writer thread
Reader reads by calling methods like get
on states
with no synchronized
or any kinds of barriers.
Writer thread writes by reassigning states
to new variables like states = ImmutableMap.of(...)
in a synchronized
method (which, as far as I know, provides no guarantee of visibility in the other threads if they have no read memory barriers).
So the whole thing looks like this:
State read(String key) {
return states.get(key);
}
synchronized void write(String key, State value) {
var hashMap = new HashMap<String, State>();
states.forEach(hashMap::put);
hashMap.put(key, value);
states = ImmutableMap.of(hashMap);
}
Is it true, that reader, when reading from a field states
gets a valid value of states
, that existed at one point of the time (linearizability is present)? Can reader thread read some nonsense when reading value of states
due to compiler reordering?
1 Answer
Reset to default 2Is it true, that reader, when reading from a field states gets a valid value of states, that existed at one point of the time (linearizability is present)? Can reader thread read some nonsense when reading value of states due to compiler reordering?
In your example each read of states
has no happens-before ordering with writes to states
.
According to the JMM that means every read is allowed to return any of the writes to states
that happen anytime during the program execution.
This includes writes to states
that happen later (in wall-clock time) in the same execution.
This also includes null
- the default initialization value for states
.
Here's the quote from the JLS:
Informally, a read r is allowed to see the result of a write w if there is no happens-before ordering to prevent that read.
To sum up:
- linearizability isn't present
- regarding "nonsense" I'd call it limited:
- on the one hand, you don't get undefined behavior like in C\C++
- on the other hand, the fact that a read is allowed to return any (including future) write to the variable - that alone causes huge amount of weird concurrency bugs in practice.
本文标签: javaAre operations to ImmutableCollection saved to nonfinal field threadsafeStack Overflow
版权声明:本文标题:java - Are operations to ImmutableCollection saved to non-final field thread-safe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744070147a2585732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
read
will always see a "valid" state snapshot, but that it could be stale. Nothing in the code (that you showed us) guarantees that the update to thestate
field inwrite
will be visible inread
. – Stephen C Commented Mar 27 at 23:49state
to bevolatile
. – Stephen C Commented Mar 27 at 23:59