Java util之常用数据类型特性盘点(Map 四种同步方式的性能比较)
出处:http://www.iteye.com/topic/164644
?
?
- 使用 synchronized 关键字,这也是最原始的方法。代码如下?
?
synchronized(anObject){value = map.get(key);}??
?
使用 JDK1.5 提供的锁(java.util.concurrent.locks.Lock)。代码如下??
lock.lock();value = map.get(key);lock.unlock();?
?
?
实际应用中,可能多数操作都是读操作,写操作较少。针对这种情况,可以使用 JDK1.5 提供的读写锁(java.util.concurrent.locks.ReadWriteLock)。代码如下??
rwlock.readLock().lock();value = map.get(key);rwlock.readLock().unlock();?
?
使用 JDK1.5 提供的 java.util.concurrent.ConcurrentHashMap 类。该类将 Map 的存储空间分为若干块,每块拥有自己的锁,大大减少了多个线程争夺同一个锁的情况。代码如下??
value = map.get(key); //同步机制内置在 get 方法中?
?
结论:?写了段测试代码,针对这四种方式进行测试,结果见附图。测试内容为 1 秒钟所有 get 方法调用次数的总和。为了比较,增加了未使用任何同步机制的情况(非安全!)。理论上,不同步应该最快。?
我的 CPU 是双核的(Core 2 Duo E6300),因此太多线程也没啥意义,所以只列出了单线程、两个线程和五个线程的情况。更多线程时,CPU 利用率提高,但增加了线程调度的开销,测试结果与五个线程差不多。?

从附图可以看出:?
1、不同步确实最快,与预期一致。?
2、四种同步方式中,ConcurrentHashMap 是最快的,接近不同步的情况。?
3、synchronized 关键字非常慢,比使用锁慢了两个数量级。真是大跌眼镜,我很迷惑为什会 synchronized 慢到这个程度。?
4、使用读写锁的读锁,比普通所稍慢。这个比较意外,可能硬件或测试代码没有发挥出读锁的全部功效。
?
因此总结如下:
1、如果 ConcurrentHashMap 够用,则使用 ConcurrentHashMap。?
2、如果需自己实现同步,则使用 JDK1.5 提供的锁机制,避免使用 synchronized 关键字。?
?
?
上图为原作者的数据,下图是我的机器实测数据:
CPU:Core 2 Duo T6570 双核内存2G
之所以增加了一个16线程,是因为ConcurrentHashMap使用了16个分离锁。。好象是。。。
import java.util.*;import java.util.concurrent.*;import java.util.concurrent.locks.*;public class MapTest{public static final int THREAD_COUNT = 1;public static final int MAP_SIZE = 1000;public static final int EXECUTION_MILLES = 1000;public static final int[] KEYS = new int[100]; public static void main(String[] args) throws Exception{//初始化Random rand = new Random();for (int i = 0; i < KEYS.length; ++i){KEYS[i] = rand.nextInt();}//创建线程long start = System.currentTimeMillis();Thread[] threads = new Thread[THREAD_COUNT];for (int i = 0; i < THREAD_COUNT; ++i){//threads[i] = new UnsafeThread();//threads[i] = new SynchronizedThread();//threads[i] = new LockThread();//threads[i] = new ReadLockThread();threads[i] = new ConcurrentThread();threads[i].start();}//等待其它线程执行若干时间Thread.sleep(EXECUTION_MILLES);//统计 get 操作的次数long sum = 0;for (int i = 0; i < THREAD_COUNT; ++i){sum += threads[i].getClass().getDeclaredField("count").getLong(threads[i]);}long millisCost = System.currentTimeMillis() - start;System.out.println(sum + "(" + (millisCost) + "ms)");System.exit(0);}public static void fillMap(Map<Integer, Integer> map){Random rand = new Random();for (int i = 0; i < MAP_SIZE; ++i){map.put(rand.nextInt(), rand.nextInt());}}}class UnsafeThread extends Thread{private static Map<Integer, Integer> map = new HashMap<Integer, Integer>();public long count = 0;static{MapTest.fillMap(map);}public void run(){for (;;){int index = (int)(count % MapTest.KEYS.length);map.get(MapTest.KEYS[index]);++count;}}}class SynchronizedThread extends Thread{private static Map<Integer, Integer> map = new HashMap<Integer, Integer>();public long count = 0;static{MapTest.fillMap(map);}public void run(){for (;;){int index = (int)(count % MapTest.KEYS.length);synchronized(SynchronizedThread.class){map.get(MapTest.KEYS[index]);}++count;}}}class LockThread extends Thread{private static Map<Integer, Integer> map = new HashMap<Integer, Integer>();private static Lock lock = new ReentrantLock();public long count = 0;static{MapTest.fillMap(map);}public void run(){for (;;){int index = (int)(count % MapTest.KEYS.length);lock.lock();map.get(MapTest.KEYS[index]);lock.unlock();++count;}}}class ReadLockThread extends Thread{private static Map<Integer, Integer> map = new HashMap<Integer, Integer>();private static Lock lock = new ReentrantReadWriteLock().readLock();public long count = 0;static{MapTest.fillMap(map);}public void run(){for (;;){int index = (int)(count % MapTest.KEYS.length);lock.lock();map.get(MapTest.KEYS[index]);lock.unlock();++count;}}}class ConcurrentThread extends Thread{private static Map<Integer, Integer> map = new ConcurrentHashMap<Integer, Integer>();public long count = 0;static{MapTest.fillMap(map);}public void run(){for (;;){int index = (int)(count % MapTest.KEYS.length);map.get(MapTest.KEYS[index]);++count;}}}?
?
?
?
?
?
?
?
2011-02-24david.wang