Java Reference
In-Depth Information
Listing 11.7. ServerStatus Refactored to Use Split Locks.
11.4.3. Lock Striping
Splitting a heavily contended lock into two is likely to result in two heavily contended locks.
While this will produce a small scalability improvement by enabling two threads to ex-
ecute concurrently instead of one, it still does not dramatically improve prospects for concur-
rency on a system with many processors. The lock splitting example in the ServerStatus
classes does not offer any obvious opportunity for splitting the locks further.
Lock splitting can sometimes be extended to partition locking on a variablesized set of inde-
pendent objects, in which case it is called lock striping . For example, the implementation of
ConcurrentHashMap uses an array of 16 locks, each of which guards 1/16 of the hash
buckets; bucket N is guarded by lock N mod 16. Assuming the hash function provides reas-
onable spreading characteristics and keys are accessed uniformly, this should reduce the de-
mand for any given lock by approximately a factor of 16. It is this technique that enables
ConcurrentHashMap to support up to 16 concurrent writers. (The number of locks could
be increased to provide even better concurrency under heavy access on high-processor-count
systems, but the number of stripes should be increased beyond the default of 16 only when
Search WWH ::




Custom Search