Java同步问题
我是 Java 线程和同步的新手。
假设我有:
public class MyClass(){
public synchronized void method1(){
//call method2();
}
public synchronized void method2(){};
}
当我在实例对象上同步
method1()
时意味着什么?因此,当一个线程在尝试访问synchronized method1()
时获取锁时,是否会阻止其他线程从同一对象访问另一个synchronized method2()
?假设一个线程在访问 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(){};
}
What does it mean when I synchronize a
method1()
on an instance object? So when a thread acquired the lock when trying to access thesynchronized method1()
, does it prevent other threads to access anothersynchronized method2()
from that same object?Lets say a thread acquires a lock when accessing method1(), but lets say that
method1()
makes a call tomethod2()
which is alsosynchronized
. Can this be possible? I mean are there any rules that can preventmethod1()
from callingmethod2()
?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.请参见此处:
<块引用>
同一对象上的同步方法的两次调用不可能交错。当一个线程正在执行对象的同步方法时,调用同一对象的同步方法的所有其他线程都会阻塞(挂起执行),直到第一个线程完成该对象。
由于该线程持有当前对象的锁,因此它可以调用
method2()
,而其他线程则不能。See here:
Since this thread holds the lock on the current object, it can invoke
method2()
, and no other thread can.关于问题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.
(1) 这相当于:
因此它在当前实例上进行同步。如果我们以同样的方式重写method2……
那么您可以清楚地看到它们锁定了同一个对象,因此其他线程无法调用method1或method2,直到method1退出其
synchronized
块。(2) 同步块是可重入的,这意味着同一线程可以多次进入锁定同一对象的其他同步块。据我了解,每次进入同步块时,Java 都会将正在同步的对象的计数器加 1,每次退出同步块时,它减少了它。当计数器达到 0 时,锁被释放。
(1) This is equivalent to:
So it synchronizes on the current instance. If we rewrite method2 in the same way...
... 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 othersynchronized
blocks that lock on the same object as many times as it wants. As I understand it, every time you enter asynchronized
block, Java increases a counter on the object you are synchronizing on by 1, and every time you exit asynchronized
block, it decreases it. When that counter reaches 0, the lock is released.