Java Reference
In-Depth Information
you have evidence that concurrent writers are generating enough contention to warrant rais-
ing the limit.)
One of the downsides of lock striping is that locking the collection for exclusive access is
more difficult and costly than with a single lock. Usually an operation can be performed by
acquiring at most one lock, but occasionally you need to lock the entire collection, as when
ConcurrentHashMap needs to expand the map and rehash the values into a larger set of
buckets. This is typically done by acquiring all of the locks in the stripe set. [10]
StripedMap in Listing 11.8 illustrates implementing a hash-based map using lock striping.
There are N_LOCKS locks, each guarding a subset of the buckets. Most methods, like get ,
need acquire only a single bucket lock. Some methods may need to acquire all the locks but,
as in the implementation for clear , may not need to acquire them all simultaneously. [11]
11.4.4. Avoiding Hot Fields
Lock splitting and lock striping can improve scalability because they enable different threads
to operate on different data (or different portions of the same data structure) without inter-
fering with each other. A program that would benefit from lock splitting necessarily exhib-
its contention for a lock more often than for the data guarded by that lock. If a lock guards
two independent variables X and Y , and thread A wants to access X while B wants to access
Y (as would be the case if one thread called addUser while another called addQuery in
ServerStatus ), then the two threads are not contending for any data, even though they
are contending for a lock.
Search WWH ::




Custom Search