[concurrency-interest] dealing with people that question visibility problems

Gregg Wonderly gergg at cox.net
Wed Feb 21 11:47:59 EST 2007


Peter Veentjer wrote:
> Does someone have experience with dealing with this situation?

On a single core systems, and prior to JDK 1.5 (and some 1.4) simple (incorrect) 
code such as

public class MyThread implements Runnable {
     private boolean done;
     public void shutdown() {
	done = true;
     }

     public void run() {
         while( !done ) {
             ...
         }
     }
}

will work just fine.  Now days, you will have visibility problems with changes 
in the value of "done".  I recently had a conversation with a research professor 
who said that his current mantra is that all class fields must either be "final" 
or "volatile".  If a field doesn't have one of those qualifiers on it, it will 
be problematic and the overhead of volatile is far less of a concern than 
incorrect code.

There are probably a million or more ways to demonstrate visibility problems for 
the inexperienced to learn from.  Maybe someone can create a nice set of 
graphical demos that show the problem literally.

Gregg Wonderly


More information about the Concurrency-interest mailing list