0

Recently my friend was asked this question in an Interview.

Question: We have class A with 2 methods X1 and X2. X1 is static synchronized and X2 is synchronized.

class A {
    public static synchronized X1() {
    }
    public synchronized X2() {
    }
}

Now we know that static synchronized attains the lock on CLASS and synchronized attains the lock on calling object.

Interviewer asked that "what do you mean by lock on CLASS?". The lock is always done on Object. Then what actually happen in case of static synchronized. It attains lock on WHICH OBJECT?

Can someone clear this concept for me as its getting a little confusing out here.

Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
learner
  • 1,095
  • 2
  • 18
  • 41
  • Don't say "static synchronized". "static" does something much more funamental than just specifying which mutex gets locked when the method is called. – Solomon Slow May 12 '14 at 16:36
  • What I should have said: You make it sound like "static" is there to change the meaning of "synchronized". It _does_ do that, but it's only a side effect of its broader meaning; The real meaning of "static" is that the method is called without reference to any instance of the class (i.e., there is no _this_ reference). Since there's no _this_, then obviously the method can not synchronize on _this_. It synchronizes on the class object instead because that's the next most obvious choice. – Solomon Slow May 12 '14 at 17:28

2 Answers2

3

According to Oracle concurrency tutorial:

You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.

Ale Sequeira
  • 2,039
  • 1
  • 11
  • 19
-1

As this SO answer exactly points

there is no link between synchronized static methods and sync'ed non static methods in 
this context

To prove this.

public class SimpleClassTest {

Static Sync method

public static synchronized void X1() {
        System.out.println("Before X1 Exec.. From"+Thread.currentThread().getName());
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("After X1 Exec.. From"+Thread.currentThread().getName());
    }

Non Static Sync Method

public synchronized void X2() {
        System.out.println(" X2 Exec.. From"+Thread.currentThread().getName());
    }

First method simply sleep for some time and wake up. Second method sync but not static .

Now try to create two threads and call this method same time ( approx)

public static void main(String args[]){
        final SimpleClassTest instance = new SimpleClassTest();

First thread for static

    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            SimpleClassTest.X1();
        }
    },"Thread1");

Second thread for non static

    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            instance.X2();
        }
    },"Thread2");

Now start both the threads

    t1.start();
    t2.start();

And result

Before X1 Exec.. FromThread1
 X2 Exec.. FromThread2
After X1 Exec.. FromThread1

From the result both Threads running without locking each other. Because if the first thread locks the same object on which second thread locking then thread 2 would wait until the first thread completes .

So from this we are sure both are running in parallel.

Firs thread has lock on SimpleClasstest.class since it is static method nothing to do with instance.

Second thread has lock on instance since it is non static.

Hope it clears

Community
  • 1
  • 1
Mani
  • 3,274
  • 2
  • 17
  • 27
  • down voters , please comment. so that i can find mistake if any – Mani May 12 '14 at 15:38
  • The question was: "Which object's intrinsic lock is acquired by a thread that calls a synchronized static method of a class?". I don't see why your dozens lines of code answer this question. (The answer should rather be something like: "The thread acquires the lock for the class object associated with the class. So in your case it's equivalent to: synchronized(A.class) {...}"). – isnot2bad May 12 '14 at 15:47
  • As has become classic... I didn't downvote, but I imagine it is because you have answered the wrong question. It wasn't "Are the locks on the same Object?", but rather "What does the static version lock on?", which you touched briefly in just a single line with no explanation. – Ordous May 12 '14 at 15:47
  • @isnot2bad and Ordous thanks for the comment. I thought the other answer provides the perfect link to clarify that. THought of providing some examples on what would happen. Now i understand the reason. i Should have addressed only about lock on "SimpleClasstest.class" – Mani May 12 '14 at 15:53