Note that there are some explanatory texts on larger screens.

plurals
  1. POGiven that HashMaps in jdk1.6 and above cause problems with multi=threading, how should I fix my code
    primarykey
    data
    text
    <p>I recently raised a question in stackoverflow, then found the answer. The initial question was <a href="https://stackoverflow.com/questions/13969076/what-mechanisms-other-than-mutexs-or-garbage-collection-can-slow-my-multi-thread/14003357#14003357">What mechanisms other than mutexs or garbage collection can slow my multi-threaded java program?</a></p> <p>I discovered to my horror that HashMap has been modifed between JDK1.6 and JDK1.7. It now has a block of code that causes all threads creating HashMaps to synchronize.</p> <p>The line of code in JDK1.7.0_10 is</p> <pre><code> /**A randomizing value associated with this instance that is applied to hash code of keys to make hash collisions harder to find. */ transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this); </code></pre> <p>Which ends up calling</p> <pre><code> protected int next(int bits) { long oldseed, nextseed; AtomicLong seed = this.seed; do { oldseed = seed.get(); nextseed = (oldseed * multiplier + addend) &amp; mask; } while (!seed.compareAndSet(oldseed, nextseed)); return (int)(nextseed &gt;&gt;&gt; (48 - bits)); } </code></pre> <p>Looking in other JDKs, I find this isn't present in JDK1.5.0_22, or JDK1.6.0_26. </p> <p>The impact on my code is huge. It makes it so that when I run on 64 threads, I get less performance than when I run on 1 thread. A JStack shows that most of the threads are spending most of their time spinning in that loop in Random.</p> <p>So I seem to have some options:</p> <ul> <li>Rewrite my code so that I don't use HashMap, but use something similar</li> <li>Somehow mess around with the rt.jar, and replace the hashmap inside it</li> <li>Mess with the class path somehow, so each thread gets its own version of HashMap</li> </ul> <p>Before I start down any of these paths (all look very time consuming and potentially high impact), I wondered if I have missed an obvious trick. Can any of you stack overflow people suggest which is the better path, or perhaps identify a new idea.</p> <p>Thanks for the help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload