JavaMemoryModel: Empty synchronized blocks are not useless (?)

From: Doron Rajwan (doron@rajwan.org)
Date: Wed Apr 14 2004 - 11:42:32 EDT


As I understand the JMM, an empty synchronized block
is not automatically uesless. For example, the test
below is well-synchronized. Is this correct?

class Obj {
    private static Object SOMETHING = new Object();
    private int x;
    public void setX(int x) {
        this.x = x;
        synchronized(SOMETHING) {}
    }
    public int getX() {
        synchronized(SOMETHING) {}
        return x;
    }
}

Initially, global = null.

Thread 1:
    Obj obj = new Obj();
    obj.setX(3);
    global = obj;

Thread 2:
    Obj obj = global;
    if (obj != null)
        System.out.println(obj.getX());

Can this code print 0?

Doron.

=====
Doron Rajwan, mailto:doron@rajwan.org

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



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