Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
According to Goetz and colleagues \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\]:

Client-side locking entails guarding client code that uses some object X with the lock X uses to guard its own state. In order to use client-side locking, you must know what lock X uses.

Wiki Markup
While client-side locking is acceptable when the thread-safe class commits to and clearly documents its locking strategy, Goetz and colleagues caution against its misuse \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\]:

If extending a class to add another atomic operation is fragile because it distributes the locking code for a class over multiple classes in an object hierarchy, client-side locking is even more fragile because it entails putting locking code for class C into classes that are totally unrelated to C. Exercise care when using client-side locking on classes that do not commit to their locking strategy.

Wiki Markup
The documentation of a class that supports client-side locking should explicitly state its applicability. For example, the class {{java.util.concurrent.ConcurrentHashMap<K,V>}} should not be used for client-side locking because its documentation \[[API 2006|AA. Bibliography#APIReferences#API 06]\] states that:

However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details.

Wiki Markup
Use of client-side locking is permitted only when the documentation of the class recommends it. For example, the documentation of the {{synchronizedList()}} wrapper method of {{java.util.Collections}} class \[[API 2006|AA. Bibliography#APIReferences#API 06]\] states 

In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. It is imperative that the user manually synchronize on the returned list when iterating over it. Failure to follow this advice may result in non-deterministic behavior.

...

Wiki Markup
Goetz and colleagues describe the fragility of class extension for adding functionality to thread-safe classes \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\]:

Extension is more fragile than adding code directly to a class, because the implementation of the synchronization policy is now distributed over multiple, separately maintained source files. If the underlying class were to change its synchronization policy by choosing a different lock to guard its state variables, the subclass would subtly and silently break because it no longer used the right lock to control concurrent access to the base class's state.

...

Wiki Markup
If the {{IPAddressList}} class were modified to use block synchronization on a private final lock object, as recommended by rule [LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code], the {{PrintableIPAddressList}} subclass would silently break. Moreover, if a wrapper such as {{Collections.synchronizedList()}} were used, it would be difficult for a client to determine the type of the class being wrapped to extend it \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\].

Compliant Solution (Composition)

...

Wiki Markup
In this case, composition allows the {{PrintableIPAddressList}} class to use its own intrinsic lock independent of the underlying list class's lock. The underlying collection lacks a requirement for thread-safety because the {{PrintableIPAddressList}} wrapper prevents direct access to its methods by publishing its own synchronized equivalents. This approach provides consistent locking even when the underlying class changes its locking policy in the future \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\].

Risk Assessment

Using client-side locking when the thread-safe class fails to commit to its locking strategy can cause data inconsistencies and deadlock.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="75d9dfbb8068719f-910c437b-47f149bd-8919ad3c-19b0f14376c94a4e40708bce"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API References#API 06]]

Class Vector, Class WeakReference, Class ConcurrentHashMap<K,V>

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="59b22243136f1417-1cd9b789-430148f8-ab47b6f6-e995620aef84a65a922d12ed"><ac:plain-text-body><![CDATA[

[[JavaThreads 2004

AA. Bibliography#JavaThreads References#JavaThreads 04]]

8.2, Synchronization and Collection Classes

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="825930beb93e0575-d17122b8-43f24eb8-b9019e0f-4090f5cba43203148426956c"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. Bibliography#Goetz References#Goetz 06]]

4.4.1, Client-side Locking; 4.4.2, Composition; and 5.2.1, ConcurrentHashMap

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="774218471d63f7ae-09105e04-43284644-ab09991f-634bb75175f9e12059d2aa79"><ac:plain-text-body><![CDATA[

[[Lee 2009

AA. Bibliography#Lee References#Lee 09]]

Map & Compound Operation

]]></ac:plain-text-body></ac:structured-macro>

...