Last active
December 10, 2015 06:58
-
-
Save punya/4397524 to your computer and use it in GitHub Desktop.
Example showing why creating HashMaps in many threads is slower under JDK7 than under JDK6.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.punya.threadlocalrandomnumbers; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ExecutorService; | |
import java.util.HashMap; | |
import org.junit.Test; | |
public final class ParallelHashMapCreationTest { | |
private static final int THREADS = 16; | |
private static final int MAPS_PER_THREAD = 1000000; | |
@Test | |
public void benchmark() throws InterruptedException { | |
final CountDownLatch latch = new CountDownLatch(THREADS); | |
class CreateMapsTask implements Runnable { | |
@Override | |
public void run() { | |
for (int i = 0; i < MAPS_PER_THREAD; ++i) { | |
new HashMap<String, String>(); | |
} | |
latch.countDown(); | |
} | |
} | |
ExecutorService exec = Executors.newFixedThreadPool(THREADS); | |
for (int i = 0; i < THREADS; ++i) { | |
exec.submit(new CreateMapsTask()); | |
} | |
latch.await(); | |
exec.shutdownNow(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment