JavaMemoryModel: Unsynchronized read as an optimization

From: Robert Munyer (munyer@digi-net.com)
Date: Fri Dec 15 2000 - 13:50:52 EST


I've been discussing double-check with the author of a high-performance Java
web server I'm evaluating. He believes unsynchronized read is safe in the
majority of JVM installations (even if you only count servers) and he would
like to have an optimization setting to take advantage of this feature.

The idea is to differentiate between JVMs that take full advantage of the
flexibility allowed by Chapter 17, and other JVMs that use hardware and/or
software to guarantee that when a thread first reads a variable it will not
receive a value that's older than the one in main memory.

After some thought, I think the idiom below will serve this purpose. I want
to ask two questions of the experts on this list: (1) Is this code correct?
And if the answer to #1 is yes: (2) Can you give me an idea of which JVMs
(current and imminent) will be unable to take advantage of the optimization?

                                Thanks,

                                Robert Munyer <munyer@digi-net.com>

class Foo {
  private Helper helper = null;
  private boolean helperExists;
  public Helper getHelper() {
    if (!helperExists)
      synchronized(this) {
        if (helper == null)
          helper = new Helper();
        else
          helperExists = GlobalSettings.ALLOW_UNSYNCHRONIZED_READ;
      }
    return helper;
  }
  // other functions and members...
}

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:29 EDT