Re: JavaMemoryModel: Idiom for safe, unsynchronized reads

From: Joshua Bloch (
Date: Mon Jun 28 1999 - 14:56:38 EDT


   Thanks for the succinct, concise summary of our positions. I believe
that it accurately summarizes our differences. Simply put, you believe that
programmers should have to synchronize all accesses to shared data, I
believe that programmers should not have to synchronize access to immutable

    To reiterate and clarify my position, I believe that programmers
intuitively understand that they needn't synchronize access to immutable
objects. I believe that any memory model that violates this intuition will
lead to subtle bugs that only show up on MP's with "aggressive" memory
consistency models. I don't think that it's worth taking this risk,
especially given that there is controversy among experts in the field as to
whether such multiprocessor designs are reasonable.

   People (including Bill Joy and Guy Steele, who wrote the memory model)
are shocked, when they find out that according to the model, this code
returns a potentially corrupt String:

    static String foo = null;

    String getFoo() {
        if (foo == null)
            foo = new String(..whatever..);
        return foo;

Theoretically, attempting to use the returned String could result in
arbitrary, nondeterministic behavior. People assume that Strings (being
immutable) aren't susceptible to this sort of thing, and they feel grossly
violated when they learn otherwise. I'm doubtful as to whether people will
ever learn to live with this. If our memory model allows it, we're letting
implementation dictate interface, which we all believe is generally a bad



   In regards to this note:

> (BTW, "synchronizing operations" needs to be
>expanded beyond "lock" and "unlock" to include things like thread creation
>and class loading. But this is a story for another post...)

   It is my recollection that the JLS *does* take care of class loading,
thought it's not mentioned in Chapter 17. It's buried in the detailed
description of the class loading procedure. Doug Lea brought this to my
attention when I said the same thing that you did.

This is the JavaMemoryModel mailing list, managed by Majordomo 1.94.4.

To send a message to the list, email
To send a request to the list, email and put
your request in the body of the message (use the request "help" for help).
For more information, visit

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