英文:
Does the lock on a static synchronized method affect the non-static synchronized methods of it's instances?
问题
假设我有两个方法,一个声明为synchronized
,另一个声明为static synchronized
。因此,当一个线程获取类级别的锁时,它是否也会获取所有实例的锁?换句话说,如果一个线程获取了类级别的锁,是否可以同时让另一个线程获取其某个实例级别的对象锁?
英文:
Suppose I have 2 methods, one declared as synchronized
and the other declared as static synchronized
.
So when a thread acquires the class-level lock, does it acquire the locks on all its instances as well? In other words, if a thread acquires a class-level lock, can another thread acquire an object-level lock on one of its instances simultaneously?
答案1
得分: 6
一个static synchronized
方法会获取该类的Class
实例上的锁。一个synchronized
方法会获取this
上的锁。当您通过调用同步静态方法获取类级别的锁时,对象级别的锁不受影响。
英文:
A static synchronized
method will acquire the lock on the Class
instance for the class. A synchronized
method will acquire the lock on this
. When you acquire the class level lock by calling a synchronized static method, the object-level locks are not affected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论