public synchronized int getCountOne() {
return count++;
}
与上述代码一样,在该方法上的同步在功能上等同于在该方法的主体周围具有synchronized (this) block
。对象“this”不会被锁定,而是使用对象“this”作为mutex
,并且主体被阻止与同时在“this”上同步的其他代码段同时执行。如何获取课程等级锁
基于相同的理由,当我们收购,如果我们有一个函数的类级lock.As
public static synchronized int getCountTwo() {
return count++;
}
显然两个线程可以同时获得getCountOne锁(对象级锁),什么是用作mutex
getCountTwo(班级锁定)。因此getCountOne类似于
public int getCountOne() {
synchronized(this) {
return count++;
}
}
是否有等价的getCountTwo?如果没有什么标准被用来获得一个级别锁?
感谢您的好解释 – Prabhaker