6

When a Java member needs to be thread-safe, we do like the following:

 public synchronized void func() {
     ...
 }

This syntax equivalent to:

 public void func() {
      synchronized(this) {
           ....
      }
 }

That is, it actually uses this for a lock.

My question is, if I use synchronized with a static method, as follows:

class AA {
    private AA() {}

    public static synchronized AA getInstance() {
        static AA obj = new AA();
        return obj;
    }
}

In this case, on what is the lock made for the synchronized method?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
TieDad
  • 9,143
  • 5
  • 32
  • 58

3 Answers3

13

In case of static synchronized method, the class object of your class AA will be implicit lock

its equivalent to

class AA {
    private AA() {}

    public static AA getInstance() {
        synchronized(AA.class) {
           AA obj = new AA();
           return obj;
        }
    }
}
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • 3
    +1. equivalent to `synchronized (AA.class) { ` – Thilo Jul 16 '13 at 05:48
  • but what about `static AA obj = new AA();` - wouldn't it give compile time error ? – exexzian Jul 16 '13 at 06:00
  • thanks @Bingo i have corrected it now.. I had not paid attention to that code – sanbhat Jul 16 '13 at 06:10
  • 1
    @sanbhat well it happens, but u surely deserve upvote +1 – exexzian Jul 16 '13 at 06:11
  • @sanbhat So when we synchronze on `this`, another object cannot execute the synchronized thread, but when synchronized on class? What happens exactly? True that it acquired the lock of the class; does that mean another class cannot execute the synchronized piece of code? – Prasad Kharkar Jul 16 '13 at 06:16
  • @PrasadKharkar since the lock object is the `Class` object associated with the class, and there will be only **one** `Class` object, only one thread can execute `static synchronized` methods of a class **at any instance of time**. To make multiple methods of a same class, execute in a mutually exclusive manner, all the method can be declared as `static synchronized` – sanbhat Jul 16 '13 at 06:32
  • @sanbhat, got it :) thanks but what is the use of it? I mean is there any practical use for it? sorry if its a dumb question. But I would like to know – Prasad Kharkar Jul 16 '13 at 07:10
  • An old blog written by me i hope should help.. http://bhat86.blogspot.in/2011/06/mutual-exclusion-among-2-different.html – sanbhat Jul 16 '13 at 09:01
7

From section 8.4.3.6 of the JLS:

A synchronized method acquires a monitor (§17.1) before it executes.

For a class (static) method, the monitor associated with the Class object for the method's class is used.

So your code acquires the monitor for AA.class. As sanbhat says, it's like

synchronized(AA.class) {
    ...
}

... just as with an instance method it would be

synchronized(this) {
    ...
}
Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

It worked on AA.class lock.

public static AA getInstance() {
        synchronized(AA.class){
            static AA obj = new AA();
            return obj;
        }

}
lichengwu
  • 4,277
  • 6
  • 29
  • 42