public synchronized int getCountOne() {
return count++;
}
synchronized (this) block
上記のコードのように、メソッドで同期することは、メソッドの本体を囲むことと機能的に同等です。オブジェクト "this" はロックされず、オブジェクト "this" が として使用されmutex
、本体が "this" で同期されている他のコード セクションと同時に実行されないようにします。
同様の理由でmutex
、クラス レベル ロックを取得するときにとして使用されるもの。
public static synchronized int getCountTwo() {
return count++;
}
obviously two threads can simultaneously obtain locks on getCountOne(object level lock) and getCountTwo(class level lock). So as getCountOne is analogous to
public int getCountOne() {
synchronized(this) {
return count++;
}
}
is there an equivalent of getCountTwo? If no what criteria is used to obtain a Class level lock?