Java监视器是否包含实例变量?


问题内容

Java中的Monitor是否不限制对实例变量的访问,而不仅限于对已声明为 同步 的方法或在 同步 语句中进行代码访问?

我创建了两个线程,thread yinvokes sync()方法,该方法声明为已 同步,thread rinvokes
unsync()方法,该方法未声明为已同步。两者都调用共享对象上的方法s

Thread r可以在对象s监视器 仍由持有者的情况下修改对象的实例变量thread y

Java中的监视器是否不限制对实例变量的访问,而不仅限于对已声明 同步 的方法或在 同步 语句中的代码的访问?

public class Stuff {

    private int a = 10;

    public synchronized void sync() {
        long t1 = System.currentTimeMillis();
        System.out.println("Okay, I am in sync() method. "
                        + "I will be waiting for 10 seconds. Current Time = "
                        + System.currentTimeMillis());
        while (System.currentTimeMillis() - t1 < 10000);
        System.out.println("Okay, I have waited for 10 seconds. Current time is "
                        + System.currentTimeMillis()
                        + ". Now I will exit from sync() method, a = " + this.a);
    }

    public void unsync() {
        System.out.println("Alright, I am in unsync() method. The current time is "
                        + System.currentTimeMillis());
        this.a = this.a + 1;
        System.out.println(". The time of exit from unsync() method is "
                        + System.currentTimeMillis());

    }
}

class T1 extends Thread {

    Stuff s;

    public T1(Stuff s) {
        this.s = s;
    }

    public void run() {
        s.sync();
    }
}

class T2 extends Thread {

    Stuff s;

    public T2(Stuff s) {
        this.s = s;
    }

    public void run() {
        s.unsync();
    }
}

class Main {

    public static void main(String args[]) throws Exception {
        Stuff s = new Stuff();
        T1 y = new T1(s);
        T2 r = new T2(s);
        y.start();
        Thread.sleep(2000);
        r.start();
    }
}

该程序的输出如下:

好的,我在sync()方法中。我将等待10秒钟。当前时间= 1358801766310  
好吧,我在unsync()方法中。当前时间为1358801768343。退出unsync()方法的时间为1358801768343  
好吧,我已经等了十秒钟。当前时间是1358801776310。现在我将从sync()方法退出,a = 11

问题答案:

是。持有对象的监视器可防止另一个线程执行另一个代码块或在同一对象上同步。如果某个方法未同步,则无论另一个线程是否持有监视器,任何线程都可以随时调用它。

如果有机会至少有一个线程修改此共享状态,则对共享的已声明状态(甚至是只读访问)的每次访问都必须同步。