The synchronized
keyword is used to acquire a mutual-exclusion lock so that no other thread can acquire the lock, while it is being held by the executing thread. Recall that there are two ways to synchronize access to shared mutable variables, method synchronization and block synchronization. Excessive method synchronization can induce a denial of service (DoS) vulnerability because another class whose member locks on the class object, can fail to release the lock promptly. However, this requires the victim class to be accessible to from the hostile class.
Wiki Markup |
---|
The _private lock object_ idiom can be used to prevent the DoS vulnerability. The idiom consists of a {{private}} object declared as an instance field. The {{private}} object must be explicitly used for locking purposes in {{synchronized}} blocks, within the class's methods. This lock object belongs to an instance of the object and is not associated with the object itself. Consequently, there is no lock contention between a class method and a method of the hostile class when both try to lock on the class object. \[[Bloch 01|AA. Java References#Bloch 01]\] |
...