JDK-6404561 : (thread) ThreadLocal lock contention
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-03-27
  • Updated: 2010-08-19
  • Resolved: 2006-05-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b86Fixed
Related Reports
Relates :  
Description
The ThreadLocal implementation uses an inefficient lock approach to protecting hash code generation. This causes needless contention overhead. Replacing the lock usage with an atomic get-add provides a clear performance boost for cases where contention is significant.

Comments
SUGGESTED FIX % diff ~/1.6.0/j2se/martin/j2se/src/share/classes/java/lang/ThreadLocal.java . 66c66 < * The next hash code to be given out. Accessed only by like-named method.--- > * The next hash code to be given out. Updated atomically. 68c68,69 < private static int nextHashCode = 0; --- > private static java.util.concurrent.atomic.AtomicInteger nextHashCode = > new java.util.concurrent.atomic.AtomicInteger(); 78,82c79 < * Compute the next hash code. The static synchronization used here < * should not be a performance bottleneck. When ThreadLocals are < * generated in different threads at a fast enough rate to regularly < * contend on this lock, memory contention is by far a more serious < * problem than lock contention. --- > * Returns the next hash code. 84,87c81,82 < private static synchronized int nextHashCode() { < int h = nextHashCode; < nextHashCode = h + HASH_INCREMENT; < return h; --- > private static int nextHashCode() { > return nextHashCode.getAndAdd(HASH_INCREMENT); %
27-03-2006

EVALUATION This improvment was submitted for Mustang by a ThreadLocal author.
27-03-2006