线程中的静态同步和非静态同步方法

发布于 2024-11-01 03:08:12 字数 56 浏览 0 评论 0原文

任何人都可以解释一下这句话...“静态同步方法和非静态同步方法不会互相阻塞 - 它们可以同时运行”

can any one explain the statement ..."static synchronized method and non static synchronized method will not block each other -they can run at the same time"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦年海沫深 2024-11-08 03:08:12
static synchronized void test() { foo(); }

equals

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

while

synchronized void test() { foo(); }

equals

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

这意味着:静态方法锁定该类的类对象。非静态方法锁定调用它们的实例(默认情况下,synchronized(anyOtherLock)也是可能的)。由于它们锁定不同的对象,因此不会相互阻塞。

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.

古镇旧梦 2024-11-08 03:08:12

静态方法和非静态方法的锁对象是不同的。静态方法使用Class对象作为锁(lock obj:MyClass.class),而非静态方法则使用实例对象作为锁,此时该方法的调用是绑定(锁定对象:this)。

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).

怪我太投入 2024-11-08 03:08:12

非静态同步方法将监视器锁放在“this”上 - 这意味着仅当前对象被锁定。因此,如果任何一个线程正在访问非静态同步方法,则与当前对象关联的所有线程将被阻止访问该类的非静态同步方法。而其他对象的线程仍然可以访问这些方法。

静态同步方法在类对象上放置了监视器锁 - 这意味着如果任何对象的线程正在访问该方法,则无论任何对象如何,所有线程都将被阻止访问该类的所有静态同步方法。

公共类 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();

}

}
为静态同步运行一次,然后在可运行和运行中取消注释非静态同步调用(并注释静态同步)。你会更好地理解。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文