Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top