I am working on a project that requires the use of
Java 1.1.8 (don't ask...
we are stuck there). Old versions of Java (pre JSR-133) had some memory
model issues that I need to fully understand so that we write correct
multi-threaded code.
The following events will happen in order from top to bottom. The question
(which I will repeat below) is, is
Thread C guaranteed to see the correct
contents of X (i.e. it is a question of visibility)?
Thread A
--------
1) Creates an object X which holds immutable data
2) Places X on message queue Q0 (thereby acquiring and releasing internal lock L0)
Thread B
--------
1) Fetches object X from message queue Q0 (thereby acquiring and releasing lock L0)
2) Places X on message queue Q1 (thereby acquiring and releasing internal lock L1)
Thread C
--------
1) Fetches object X from message queue Q1 (thereby acquiring and releasing internal lock L1)
2) Inspects the immutable data fields of object X (no synchronization is performed)
My reading of Doug Lea's Java Memory Model page (
http://gee.cs.oswego.edu/dl/cpj/jmm.html) is that Thread B is guaranteed to see the up to date data of X because of the pairing of the release and acquisition of lock L0. Thread B, however, does not care about the contents of X and does not inspect it (i.e. may not force that area of memory to be read into local cache).
The question is, is Thread C guaranteed to see the correct contents of X? It was passed the object by a thread that was guaranteed (if it choose to look) to see the correct contents.
I ask the question because the guarantee is usually quite specific, there needs to be a pairing of a lock released by a thread that modified memory and an acquisition of the *same lock* by a thread that is reading that memory.
In Java 5.0+ we are fine by the "happens before" guarantee but that was introduced by JSR 133. Are we still fine (barring buggy JVMs) in Java 1.1.8?