How a static synchronized function works? [duplicate]
Asked Answered
P

3

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?

Painkiller answered 16/7, 2013 at 5:46 Comment(2)
The class AA will be locked synchronized(AA.class) , but no instancesOverflow
Isn't the static declaration of the local variable obj a syntax error?Luge
T
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;
        }
    }
}
Twinberry answered 16/7, 2013 at 5:47 Comment(8)
+1. equivalent to synchronized (AA.class) { Luge
but what about static AA obj = new AA(); - wouldn't it give compile time error ?Snavely
thanks @Bingo i have corrected it now.. I had not paid attention to that codeTwinberry
@Twinberry well it happens, but u surely deserve upvote +1Snavely
@Twinberry 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?Mockingbird
@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 synchronizedTwinberry
@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 knowMockingbird
An old blog written by me i hope should help.. bhat86.blogspot.in/2011/06/…Twinberry
A
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) {
    ...
}
Attractant answered 16/7, 2013 at 5:49 Comment(0)
H
0

It worked on AA.class lock.

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

}
Hypertensive answered 16/7, 2013 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.