abstract class Basic (){
public synchronized void basicMethod(String string){
//Some actions here
}
}
public class A extends Basic{
public void aMethod(){
//Some actions here
}
}
public class B extends Basic{
public void bMethod(){
//Some actions here
}
}
Basic a = new A();
Basic b = new B();
a.basicMethod(); // acquires lock
b.basicMethod(); //Same lock?
Run Code Online (Sandbox Code Playgroud)
换句话说 - 锁也与具体的Object或Super类有关吗?
不同的锁.甲synchronized实例方法同步与该特定对象相关联的监视器上的实例,因此每个的a和b具有其自己的显示器.
如果你有
abstract class Basic (){
public synchronized void basicMethod(String string){
//Some actions here
}
}
public class A extends Basic{
public synchronized void aMethod(){
//Some actions here
}
}
Run Code Online (Sandbox Code Playgroud)
然后调用a.basicMethod()并a.aMethod()锁定同一个监视器.