java monitor是否包含实例变量? [英] Does java monitor include instance variables?

查看:110
本文介绍了java monitor是否包含实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是java中的监视器不限制对实例变量的访问,只限于同步语句中声明为synchronized的方法或代码?

Is it that monitor in java does not restrict access to instance variables and only to the methods which are declared synchronized or code in synchronized statements?

我创建了两个threads, thread y 调用sync方法,该方法声明为synchronized,而 thread r 调用未声明同步的unsync方法。两者都在共享对象 s 上调用方法。

I have created two threads, thread y invokes sync method , which is declared synchronized while thread r invokes unsync method which is not declared synchronized. Both invoke methods on shared object s.

线程r 能够修改对象的实例变量 s 当该对象的监视器或锁定仍由线程y 持有时。

Thread r is able to modify the instance variable of object s while the monitor or lock of that object is still being held by the thread y.

是不是Java中的监视器不限制对实例变量的访问,只限于声明为synchronized的方法或同步语句中的代码?

Is it that the monitor in Java does not restrict access to instance variables, and only to the methods which are declared synchronized or code in synchronized statements?

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 currrent 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 m {
    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();
    }
}

该程序的输出如下:


Okay, I am in sync method. I will be waiting for 10 seconds. Current Time = 1358801766310
Alright, I am in unsync method the currrent time is 1358801768343
The time of exit from unsync method is 1358801768343
Okay, I have waited for 10 seconds. Current time is 1358801776310.Now I will exit from sync method,
a= 11


推荐答案

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

Yes. Holding the monitor of an object prevents another thread from executing another block of code or synchronized on the same object. If a method is not synchronized, any thread can call it at any time, whether another thread holds a monitor or not.

每次访问共享声明,甚至读取 - 如果有可能至少有一个线程修改了这个共享状态,则必须同步才能进行访问。

Every access to a shared stated, even read-only accessed, must be synchronized if there's a chance that at least one thread modifies this shared state.

这篇关于java monitor是否包含实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆