Re: JavaMemoryModel: Corner cases for final fields

From: Bill Pugh (pugh@cs.umd.edu)
Date: Tue Sep 12 2000 - 12:24:31 EDT


At 11:51 AM -0400 9/12/00, Jan-Willem Maessen wrote:
>Bill, I'm not sure I totally understand where you want to go with you
>example. Shouldn't we be encouraging programmers to write this sort
>of thing instead?
>
>class A {
> final int x;
> private static A lastA;
> A (int i) {
>// **moved** lastA = this;
> x = i;
> }
> static synchronized A makeA(int i) {
> lastA = new A(i); // ** Bind lastA in the synchronized method
> return lastA;
> }
> static synchronized A getLastA() {
> return lastA;
> }
> }
>
>-Jan-Willem Maessen
>-------------------------------
>JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel

I certainly prefer this solution. The question is whether we can tell
people that the other solution is wrong. If we get consensus that the
other version of the program is wrong, then my life becomes simpler
and I will be happy.

Let me explain what the problem is.

If we simply say:

        * A object must not be reachable by other threads until after the
          object is constructed, then we have good objects and bad objects.

        * However, if we want to allow the earlier version of class A to work,
          then we have to separate things into "good references to instances of
          A", and "bad references to instances of A". For example, I can extend
          the class I gave earlier with another method :

class A {
   final int x;
   private static A lastA;
   A (int i) {
        lastA = this;
        x = i;
        }
   static synchronized A makeA(int i) {
        return new A(i);
        }
   static synchronized A getLastA() {
        return lastA;
        }
   static A badGetLastA() {
        return lastA;
        }
   }

       * Now, the references returned by getLastA() are "good references", while
        the references returned by badGetLastA() are "bad references". We could
        even have a "good reference" and a "bad reference" to the same instance
        of A.

Having to keep track of good and bad references, rather than good and
bad objects, substantially complicates the semantics.

So I would be happy to only have to keep track of good and bad
objects. I just want everything to be aware that it means some
"reasonable" programming idioms will be declared invalid.

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



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