Java同步问题

发布于 2024-09-18 12:30:44 字数 605 浏览 6 评论 0原文

我是 Java 线程和同步的新手。

假设我有:

public class MyClass(){

    public synchronized void method1(){ 
        //call method2();
    } 

    public synchronized void method2(){};

}
  1. 当我在实例对象上同步 method1() 时意味着什么?因此,当一个线程在尝试访问 synchronized method1() 时获取锁时,是否会阻止其他线程从同一对象访问另一个 synchronized method2()

  2. 假设一个线程在访问 method1() 时获取了锁,但假设 method1() 调用了 method2(),它也是 同步。这可能吗?我的意思是是否有任何规则可以阻止 method1() 调用 method2()

提前致谢。

I'm new to Java Threads and synchronization.

Lets say I have:

public class MyClass(){

    public synchronized void method1(){ 
        //call method2();
    } 

    public synchronized void method2(){};

}
  1. What does it mean when I synchronize a method1() on an instance object? So when a thread acquired the lock when trying to access the synchronized method1(), does it prevent other threads to access another synchronized method2() from that same object?

  2. Lets say a thread acquires a lock when accessing method1(), but lets say that method1() makes a call to method2() which is also synchronized. Can this be possible? I mean are there any rules that can prevent method1() from calling method2()?

Thanks in advance.

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

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

发布评论

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

评论(4

岁月如刀 2024-09-25 12:30:44
  1. 是的,在非静态方法上使用 Synchronized 方法修饰符意味着它使用调用该方法的实例的监视器,并且这是在所有此类方法之间共享的。
  2. 否 - 线程已经拥有监视器,因此可以自由地进入受同一监视器保护的其他块。
  1. Yes, using the synchronized method modifier on a non-static method means that it uses the monitor of the instance the method is invoked on, and this is shared between all such methods.
  2. No - the thread already owns the monitor, so it is free to enter other blocks protected by the same monitor.
佼人 2024-09-25 12:30:44
  1. 请参见此处

    <块引用>

    同一对象上的同步方法的两次调用不可能交错。当一个线程正在执行对象的同步方法时,调用同一对象的同步方法的所有其他线程都会阻塞(挂起执行),直到第一个线程完成该对象。

  2. 由于该线程持有当前对象的锁,因此它可以调用method2(),而其他线程则不能。

  1. See here:

    it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

  2. Since this thread holds the lock on the current object, it can invoke method2(), and no other thread can.

哭泣的笑容 2024-09-25 12:30:44

关于问题2的注释,method1()也可以在其他类中调用synchronized方法,这可能会导致死锁:

Thread1调用synchronized method1(),而synchronized method1()又需要在AnotherClass中调用synchronized method_b()
Thread2 持有 AnotherClass 上的锁,并且正在执行一个方法,该方法需要调用 Thread1 持有锁的类中的 method1() 。

两个线程都会阻塞等待对方释放锁,这就是死锁。

A note on question 2, method1() can also call synchronized methods also in other classes which could cause a deadlock:

Thread1 calls synchronized method1() which in turn needs to call synchronized method_b() in AnotherClass
Thread2 holds the lock on AnotherClass and is executing a method that needs to call method1() in the class whose lock is held by Thread1

Both Threads will block waiting for the other to free the lock, a deadlock.

白云不回头 2024-09-25 12:30:44

(1) 这相当于:

public void method1(){ 
    synchronized (this) {
        ...
    }
}

因此它在当前实例上进行同步。如果我们以同样的方式重写method2……

public void method2(){ 
    synchronized (this) {
        ...
    }
}

那么您可以清楚地看到它们锁定了同一个对象,因此其他线程无法调用method1或method2,直到method1退出其synchronized块。

(2) 同步块是可重入的,这意味着同一线程可以多次进入锁定同一对象的其他同步块。据我了解,每次进入同步块时,Java 都会将正在同步的对象的计数器加 1,每次退出同步块时,它减少了它。当计数器达到 0 时,锁被释放。

(1) This is equivalent to:

public void method1(){ 
    synchronized (this) {
        ...
    }
}

So it synchronizes on the current instance. If we rewrite method2 in the same way...

public void method2(){ 
    synchronized (this) {
        ...
    }
}

... then you can clearly see that they lock on the same object and thus other threads could not call method1 or method2 until method1 exits its synchronized block.

(2) synchronized blocks are re-entrant, meaning that the same thread can enter other synchronized blocks that lock on the same object as many times as it wants. As I understand it, every time you enter a synchronized block, Java increases a counter on the object you are synchronizing on by 1, and every time you exit a synchronized block, it decreases it. When that counter reaches 0, the lock is released.

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