Created
May 11, 2013 18:49
-
-
Save jvermillard/5560969 to your computer and use it in GitHub Desktop.
HashMap + read lock vs ConcurentHashMap
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
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.locks.Lock; | |
import java.util.concurrent.locks.ReadWriteLock; | |
import java.util.concurrent.locks.ReentrantReadWriteLock; | |
import org.junit.Test; | |
public class Bleh { | |
@Test | |
public void testLock() { | |
ReadWriteLock lock = new ReentrantReadWriteLock(); | |
Lock readLock = lock.readLock(); | |
HashMap<String, Object> map = new HashMap<String, Object>(); | |
map.put("A", new Object()); | |
map.put("B", new Object()); | |
map.put("C", new Object()); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 1000000000; i++) { | |
readLock.lock(); | |
map.get("A"); | |
readLock.unlock(); | |
} | |
System.err.println("time : " + (System.currentTimeMillis() - start)); | |
} | |
@Test | |
public void testCH() { | |
Map<String, Object> map = new ConcurrentHashMap<String, Object>(); | |
map.put("A", new Object()); | |
map.put("B", new Object()); | |
map.put("C", new Object()); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 1000000000; i++) { | |
map.get("A"); | |
} | |
System.err.println("time : " + (System.currentTimeMillis() - start)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment