Private Lock Object Idiom-thwarts Denial-of-service attack
Private Final Object lock = new Object ();
public void Foo () {
synchronized (lock) {
...}}
}
Advantages: Prevent other clients from holding the lock object timeout, thus avoiding the client's denial of service attack;
Attention:
1, lock domain is declared final, this can prevent accidentally change its contents, and destroy the lock object;
2. The private lock object mode can only be used in an unconditional thread-safe class (the instance is mutable, but the class has enough internal synchronization, without any external synchronization) , and a conditional thread-safe class (partial method Security) cannot apply this pattern.
3. Private lock object mode is especially suitable for classes designed for inheritance, otherwise subclasses and parent classes are likely to lock each other.
See "Effective Java second Edition," article 70th.