Java Reference
In-Depth Information
thread through the operating system. Which is more efficient depends on the relationship
between context switch overhead and the time until the lock becomes available; spin-waiting
is preferable for short waits and suspension is preferable for long waits. Some JVMs choose
between the two adaptively based on profiling data of past wait times, but most just suspend
threads waiting for a lock.
Suspending a thread because it could not get a lock, or because it blocked on a condition wait
or blocking I/O operation, entails two additional context switches and all the attendant OS
and cache activity: the blocked thread is switched out before its quantum has expired, and is
then switched back in later after the lock or other resource becomes available. (Blocking due
to lock contention also has a cost for the thread holding the lock: when it releases the lock, it
must then ask the OS to resume the blocked thread.)
11.4. Reducing Lock Contention
We've seen that serialization hurts scalability and that context switches hurt performance.
Contended locking causes both, so reducing lock contention can improve both performance
and scalability.
Access to resources guarded by an exclusive lock is serialized—only one thread at a time
may access it. Of course, we use locks for good reasons, such as preventing data corruption,
but this safety comes at a price. Persistent contention for a lock limits scalability.
The principal threat to scalability in concurrent applications is the exclusive resource lock.
Two factors influence the likelihood of contention for a lock: how often that lock is requested
and how long it is held once acquired. [7] If the product of these factors is sufficiently small,
then most attempts to acquire the lock will be uncontended, and lock contention will not pose
a significant scalability impediment. If, however, the lock is in sufficiently high demand,
threads will block waiting for it; in the extreme case, processors will sit idle even though
there is plenty of work to do.
There are three ways to reduce lock contention:
•
Reduce the duration for which locks are held;
Search WWH ::




Custom Search