One of the four major changes brought to java 6 with regards to improving the performance of intrinsic locks is Adapative Spinning technique. What exactly is adaptive spinning? Is it a combination of spinlocks and mutex lock? Can someone explain in an easy to understand way the usefulness of this strategy which by default is available from JDK6 onwards.
-
1[Oracle blog](http://www.oracle.com/technetwork/java/6-performance-137236.html#2.1.3) – Anirban Nag 'tintinmj' Dec 19 '13 at 19:13
1 Answers
What exactly is adaptive spinning?
To quote from this Java 6 performance page:
Adaptive spinning is an optimization technique where a two-phase spin-then-block strategy is used by threads attempting a contended synchronized enter operation. This technique enables threads to avoid undesirable effects that impact performance such as context switching and repopulation of Translation Lookaside Buffers (TLBs). It is “adaptive" because the duration of the spin is determined by policy decisions based on factors such as the rate of success and/or failure of recent spin attempts on the same monitor and the state of the current lock owner.
So threads initially attempt to spin a couple of times trying to acquire a lock before actually blocking. Then in the future it uses previous success/fail metrics to adaptively determine whether it should try spinning or blocking. Spinning wastes CPU time while blocking may cause a context switch and the thread may wait for a longer amount of time than necessary. The goal is trying to optimize both of these issues based on past behavior.
For more details, the performance doc references this presentation entitled Synchronization in Java SE 6 (HotSpot) by Dave Dice. The first slide is titled "Contended costs (scalability + latency)":
- Context switching is extremely expensive
- Unbounded spinning is unacceptable
- Address via adaptive spinning
Later in the presentation there is the slide entitled "Adaptive Spinning":
- Spin-then-block strategy
- Try to avoid context switch by spinning on [multi-processor] systems
- Spin duration
- Maintained per-monitor
- varies based on recent history of spin success/failure ratio
- Adapts to system load, parallelism, application modality
- [multi-processor]-polite spinning
- Avoid spinning in futile conditions (owner is blocked)
Interesting stuff.

- 115,027
- 24
- 293
- 354
-
2For people interested in the details, the interesting parts in the code (for HotSpot that is) are in objectMonitor.cpp. It has actually a very good explanation of adaptive spinning in a long comment (search for "Adaptive Spinning Support") there. Was tempted to just post the whole comment but not sure how useful that would be as a whole answer, so this is the compromise. The actual code is in `ObjectMonitor::TrySpin_VaryDuration` for people interested in the gory details such as reducing bus traffic for coherency, etc. – Voo Dec 20 '13 at 00:21
-
@Gray Can you please comment on the *repopulation of Translation Lookaside Buffers (TLBs)*? What does this mean? – Geek Dec 20 '13 at 05:49
-
@Gray one more follow up related to this line : *Try to avoid context switch by spinning on **MP** systems*. What does *MP* mean? – Geek Dec 20 '13 at 06:07
-
-
TLB's are involved with memory cacheing @Geek: http://en.wikipedia.org/wiki/Translation_lookaside_buffer – Gray Dec 20 '13 at 13:24