Skip to content

Does any JVM implement blocking with spin-waiting?

In Java Concurrency in Practice, the authors write:

When locking is contended, the losing thread(s) must block. The JVM can implement blocking either via spin-waiting (repeatedly trying to acquire the lock until it succeeds) or by suspending the blocked 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 preferred 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.

When I read this I was quite surprised. Are there any known JVMs implementing blocking either on always spin-waiting or sometimes spin-waiting due to profiling results? It’s hard to believe for now.

Answer

Here is evidence that JRockit can use spinlocks – http://forums.oracle.com/forums/thread.jspa?threadID=816625&tstart=494

And if you search for “spin” in the JVM options listed here you will see evidence for the use of / support for spinlocks in Hotspot JVMs.

And if you want a current example, look at “src/hotspot/share/runtime/mutex.cpp” in the OpenJDK Java 11 source tree.