According to the Java Language Specification ,
"We say that reading r of a variable v is allowed to observe a record of w in v if, in a partial order of execution, the execution sequence:
- r is not ordered to w (i.e., this is not the case when hb (r, w) and
- there is no intermediate notation w 'on v (i.e. do not write w' on v such that hb (w, w ') and hb (w', r).
Unofficially, reading r is allowed to see the result of writing w, if this does not happen, before ordering to prevent this reading. "
This means that if there is no explicit synchronization action that causes several threads to alternate their actions in some predictable way (i.e. a good event occurs - up to the relationship defined for their actions), then the stream is allowed to see almost any value of the variable at any point where it was recorded.
If you synchronize on several different objects, it doesnβt happen - until the relationship between the reader and the author. This means that the read stream can continue to see whatever value it wants for the stopRequested variable, which can be either the first value forever, or the new value as soon as it is updated, or something delicious between them.
source share