静态同步方法上的锁是否会影响其实例的非静态同步方法?

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月1日 12:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63681244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定