简体   繁体   中英

static synchronized and non static synchronized methods in threads

可以任何人解释声明......“静态同步方法和非静态同步方法不会相互阻塞 - 它们可以同时运行”

static synchronized void test() { foo(); }

equals

static void test() { synchronized(MyClass.class) { foo(); } }

while

synchronized void test() { foo(); }

equals

void test() { synchronized(this) { foo(); } }

This means: static methods lock on the class object of the class. Non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock) is also possible). Since they lock on different objects, they don't block each other.

The lock objects are different on the static method and non-static method. The static method uses the Class object as the lock (lock obj: MyClass.class ), while the non-static method uses the instance object as the lock to which the invocation of the method at that time is bound (lock obj: this ).

non static synchronized methods put monitor lock on 'this'- It means only current object is locked.so all threads associated with current object will be blocked to access non static sync methods of that class if any one thread is accessing non static synchronized method. while other object's threads can still access the methods.

And static synchronized methods put monitor lock on Class object- It means if a thread of any object is accessing the method than all threads irrespective of any objects will be blocked to access all static synchronized methods of the class.

public class TestSync {

public synchronized void n1(int threadId)
{
    snooze(threadId);
    System.out.println("Sync non static n1 " + threadId);
}

public void n2(int threadId)
{ 
    snooze(threadId);
    System.out.println(" non static n2 " + threadId);
}

public static synchronized void s1(int threadId)
{
    snooze(threadId);
    System.out.println("Sync static s1 "+  threadId);
}

public static void s2(int threadId)
{
    snooze(threadId);
    System.out.println(" static s2 "+  threadId);
}

static void snooze(int threadId)
{
    System.out.println("Waiting ... "+ threadId);
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    TestSync ob = new TestSync();
    TestSync ob2=new TestSync();
    TestSync ob3=new TestSync();
    TestSync ob4=new TestSync();

    Runnable r1=()-> {
        /*ob.n1(10);
        ob.n2(10);*/
        ob.s1(10);
        //ob.s2(10);
    };

    Runnable r3=()-> {
        /*ob2.n1(30);
        ob2.n2(30);*/
        ob2.s1(30);
        //ob2.s2(30);
    };

    Runnable r4=()-> {
        /*ob3.n1(40);
        ob3.n2(40);*/
        ob3.s1(30);
        //ob3.s2(30);
    };

    Thread t1=new Thread(r1);
    Thread t2= new Thread(r2);
    Thread t3= new Thread(r3);
    Thread t4= new Thread(r4);
    Thread t5= new Thread(r5);
    t1.start();
    t3.start();
    t4.start();

}

} Run once for static synchronized and than uncomment the non-static synchronized calls(and comment static synchronized) in runnable and run. You will understand better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM