Blocking Locks versus Non-Blocking Locks Blocking Locks versus Non-Blocking Locks multithreading multithreading

Blocking Locks versus Non-Blocking Locks


Here's what Java Concurrency in Practice says about the subject:

The JVM can implement blocking either via spin-waiting (repeatedly trying to acquire the lock until it succeeds) or bysuspending 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 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.

And also (which is, IMO, the most important point):

Don't worry excessively about the cost of uncontended synchronization. The basic mechanism is already quite fast, and JVMs can perform additional optimizations that further reduce or eliminate the cost. Instead, focus optimization efforts on areas where lock contention actually occurs.


Well the only way to be sure is test it. When it comes to multithreading and performance you simply can't assume.