views:

258

answers:

1

In the class ReentrantReadWriteLock is the following curious comment:

transient ThreadLocalHoldCounter readHolds;

Sync() {
    readHolds = new ThreadLocalHoldCounter();
    setState(getState()); // ensures visibility of readHolds
}

what does it mean by "ensures visibility"? The reason I ask is that I have a situation where it looks as though the thread local readHolds is being reset (thread locals are implemented as WeakReferences so that shouldn't happen as long as the containing Sync object is still alive). setState/getState simply alter another instance variable and don't touch readHolds.

+2  A: 
erickson
"*any* assignments performed by the current thread—including readHolds—" are flushed. It doesn't matter if the other writes are to a volatile variable; if they come before the write to the volatile variable in the source code, they are flushed along with the volatile variable. This is one of the key changes to the Java Memory Model that occurred in Java 5.
erickson