重複の可能性:
Javaで同期(これ)を回避しますか?
2つのコードの違いは何ですか?それぞれの長所と短所は何ですか?
1)
public class Example {
    private int value = 0;
    public int getNextValue() {
        synchronized (this) {
            return value++;
        }
    }
}
2)
public class Example {
    private final Object lock = new Object();
    private int value = 0;
    public int getNextValue() {
        synchronized (lock) {
            return value++;
        }
    }
}