Consider a volatile int sharedVar. We know that the JLS gives us the following guarantees:
- every action of a writing thread
wpreceding its write of valueitosharedVarin program orderhappens-beforethe write action; - the write of value
ibywhappens-beforethe successful read ofifromsharedVarby a reading threadr; - the successful read of
ifromsharedVarby the reading threadrhappens-beforeall subsequent actions ofrin program order.
However, there is still no wall-clock time guarantee given as to when the reading thread will observe the value i. An implementation that simply never lets the reading thread see that value still complies with this contract.
I have thought about this for a while and I can’t see any loopholes, but I assume there must be. Please, point out the loophole in my reasoning.
Turns out that the answers and the ensuing discussions only consolidated my original reasoning. I now have something in the way of a proof:
Since the Java Memory Model makes no reference to wall-clock time, there will be no obstructions to this. You now have two threads executing in parallel with the reading thread observing no actions done by the writing thread. QED.
Example 1: One writing, one reading thread
To make this finding maximally poignant and real, consider the following program:
As far as JLS is concerned, this is a legal output:
Note that I don’t rely on any malfunctioning reports by
currentTimeMillis. The times reported are real. The implementation did choose, however, to make all actions of the writing thread visible only after all the actions of the reading thread.Example 2: Two threads both reading and writing
Now @StephenC argues, and many would agree with him, that happens-before, even though not explicitly mentioning it, still implies a time ordering. Therefore I present my second program that demonstrates the exact extent to which this may be so.
Just to help understanding the code, this would be a typical, real-world result:
On the other hand, you’d never expect to see anything like this, but it is still legit by the standards of the JMM:
The JVM would actually have to predict what Thread A will write at time 14 in order to know what to let the Thread B read at time 1. The plausibility and even feasibility of this is quite dubious.
From this we can define the following, realistic liberty that a JVM implementation can take:
The visibility of any uninterrupted sequence of release actions by a thread can be safely postponed until before the acquire action that interrupts it.
The terms release and acquire are defined in JLS §17.4.4.
A corrollary to this rule is that the actions of a thread which only writes and never reads anything can be postponed indefinitely without violating the happens-before relationship.
Clearing up the volatile concept
The
volatilemodifier is actually about two distinct concepts:Note the point 2. is not specified by the JLS in any way, it just kind of arises by general expectation. An implementation that breaks the promise is still compliant, obviously. With time, as we move to massively parallel architectures, that promise may indeed prove to be quite flexible. Therefore I expect that in the future the conflation of the guarantee with the promise will prove to be insufficient: depending on requirement, we’ll need one without the other, one with a different flavor of the other, or any number of other combinations.