简体   繁体   English

调用同步的静态方法,然后可以访问其他静态方法吗?

[英]invoke synchronized static method,then can other static methods be accessed?

I have an question about synchronized static methods. 我对同步静态方法有疑问。
If i invoke a synchronized static method, does it mean i lock this class and other method (including static or no static) can not be accessed before the synchronized static method end? 如果我调用同步静态方法,是否表示我锁定了此类并且在同步静态方法结束之前无法访问其他方法(包括静态或非静态)?

When A synchronized static method is locking class object, why the other static method can be invoked at the same time? 当一个同步静态方法锁定类对象时,为什么可以同时调用另一个静态方法?

class Y {
    static synchronized void staticSleep() {
        System.out.println("Start static sleep");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        System.out.println("End static sleep");
    }
    static void staticSleepNoSyn() {
        System.out.println("Start static NoSyn sleep");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        System.out.println("End static NoSyn sleep");
    }
} 

public class X {
    public static void main(String[] args) {
        for (int i = 0; i < 2; ++i) {
            new Thread(new Runnable() {

                public void run() {
                    Y.staticSleep();
                }
            }).start();
        }

        for (int i = 0; i < 10; ++i) {
            new Thread(new Runnable() {

                public void run() {
                    Y.staticSleepNoSyn();
                }
            }).start();
        }
    }
}

the output: 输出:

Start static sleep 开始静态睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
Start static NoSyn sleep 开始静态NoSyn睡眠
End static sleep 结束静态睡眠
Start static sleep 开始静态睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static NoSyn sleep 结束静态NoSyn睡眠
End static sleep 结束静态睡眠

I see what you are asking now. 我明白你现在在问什么。

If i invoke a synchronized static method, does it mean i lock this class and other method (including static or no static) can not be accessed before the synchronized static method end? 如果我调用同步静态方法,是否表示我锁定了此类并且在同步静态方法结束之前无法访问其他方法(包括静态或非静态)?

IF both methods are synchronized or use a synchronized block, AND they are synchronizing on the same thing (ie the same Class or the same this , THEN you will get mutual exclusion. 如果这两种方法都已synchronized或使用synchronized块,并且它们在同一事物(即同一Classthis上同步,那么您将互斥。

In your example, one method is not synchronized (and it doesn't use a synchronized block) so therefore, it will not be locked out. 在您的示例中,一种方法未synchronized (并且不使用synchronized块),因此不会被锁定。 In fact, nothing will lock out staticSleepNoSyn ... as you have written it. 实际上,没有什么能像您编写的那样锁定staticSleepNoSyn ...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM