1. Thread stack sizes can be reduced on machines where memory is scarce.
2. Thread stack sizes can be reduced on 32-bit JVMs to allow the heap to consume
slightly more memory within the 4 GB process size limit.
When locks are contended, the JVM (and operating system) have choices about how the lock
should be allocated. The lock can be granted fairly, meaning that each thread will be given
the lock in a round-robin fashion. Alternately, the lock can be biased toward the thread that
most recently accessed the lock.
The theory behind biased locking is that if a thread recently used a lock, the processor's
cache is more likely to still contain data the thread will need the next time it executes code
protected by that same lock. If the thread is given priority for reobtaining the lock, the prob-
ability of cache hits increases. When this works out, performance is improved. But because
biased locking requires some bookkeeping, it can sometimes be worse for performance.
In particular, applications that use a thread pool—including most application servers—often
perform worse when biased locking is in effect. In that programming model, different threads
are equally likely to access the contended locks. For these kind of applications, a small per-
formance improvement can be obtained by disabling biased locking using the -XX:-
UseBiasedLocking option. Biased locking is enabled by default.
The JVM has two options for handling a synchronized lock that is contended. The thread that
is blocked from accessing the lock can enter a busy loop, where it executes a few instructions
and then checks the lock again, or the thread can be placed in a queue somewhere and noti-
fied when the lock is available (making the CPU available for other threads).
If the contended lock is held for a short period of time, then the busy loop (known as thread
spinning) is much faster than the alternative. If the contended lock is held for a long period
of time, then it is better for the second thread to wait for a notification, which allows a third
thread to use the CPU.