Skip to content

Instantly share code, notes, and snippets.

@punya
Last active December 10, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save punya/4397524 to your computer and use it in GitHub Desktop.
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.
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