0

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
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
Start static NoSyn sleep
End static sleep
Start static sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static NoSyn sleep
End static sleep

wmmj23
  • 119
  • 1
  • 1
  • 9
  • A `static synchronized` method locks on the `Class` object for the class that declares the static method. The answer to your question follows directly from that. – Stephen C Nov 02 '14 at 10:35
  • When A synchronized static method is locking class object, why the other static method can be invoked at the same time? – wmmj23 Nov 02 '14 at 11:18
  • If the 2 methods are methods of the same class, then it can't. – Stephen C Nov 02 '14 at 11:31
  • please look at my code. Before "End static sleep" there are "Start static NoSyn sleep" printed. – wmmj23 Nov 02 '14 at 12:03
  • Only one of the methods is `synchronized`. Synchronization with mutexes only locks out other threads that are trying to synchronize using the same mutex. – Stephen C Nov 02 '14 at 12:12
  • you mean it only protect other threads accessed "the static synchronized methods" but "no static synchronized methods" ? – wmmj23 Nov 02 '14 at 12:18

1 Answers1

1

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 (i.e. the same Class or the same this, THEN you will get mutual exclusion.

In your example, one method is not synchronized (and it doesn't use a synchronized block) so therefore, it will not be locked out. In fact, nothing will lock out staticSleepNoSyn ... as you have written it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216