[concurrency-interest] Volatile array and Wait

Jeremy Manson jmanson at cs.purdue.edu
Fri Oct 21 10:40:39 EDT 2005


Mike Skells wrote:

> My point for both of these issues is not whether a JIT _does_ make these
> tranformations, as much as is there a _specification_ that stops a JIT, java
> compiler, or AOT, AOP, or native compiler for that matter, from making that
> transformation. If the limitation is just in the current implmentation then
> it is unsafe.
> 
> I cant remember seeing such a specification anywhere, although there is a
> lot of documentation out there  ;-)

There is such a specification.  The behavior of volatiles is dictated by 
the rules described in the Java memory model, which is Chapter 17 of the 
Java language specification, version 3 (as amended by JSR-133).  Java 
1.5/5.0 is compliant with this specification, although earlier versions 
are not.

The rules state that if one thread writes to a volatile, and another 
thread reads from that volatile, then all of the writes that happen 
before the write by the first thread are ordered before and visible to 
all of the reads that happen after the read by the second thread.

This requires memory synchronization and the cooperation of optimizing 
compilers.  No exception is made for redundant reads or writes.

As you say, if a variable is never read or written, it can be eliminated 
(you can deduce there are no memory effects in this case).  But, as 
David pointed out, you have to be very, very sure that the variable is 
never read or written.

					Jeremy


More information about the Concurrency-interest mailing list