IT lexicon › Programming › Lock coarsening

Lock coarsening

Programming På svenska → Updated: 2026-11-07

The JIT optimization that merges several back-to-back locks on the same object into one, so the lock is taken once instead of ten times.

The textbook Java case: ten consecutive calls to StringBuffer.append, each a synchronized method. After inlining there are ten lock/unlock pairs on the same object in a row, with nothing in between that another thread could tell apart from one big critical section. HotSpot has been folding them together since Java 6. Where lock elision deletes locks that were never needed, coarsening makes the necessary ones cheaper.

The limit is fairness. Hoisting a lock around an entire loop saves atomic operations but makes other threads wait longer, so JIT compilers are wary of merging across iterations. It is one of the few optimizations where the fastest code for one thread can be the wrong call for the program.

← Back to the lexicon