测试HashMap和ConcurrentHashMap的并发性能差别

import java.util.Collections;  
import java.util.HashMap;  
import java.util.Hashtable;  
import java.util.Map;  
import java.util.concurrent.ConcurrentHashMap;  
 
/** 
* 测试HashMap和ConcurrentHashMap的并发性能差别。 
*  
* @author 老紫竹 JAVA世纪网(java2000.net) 
*  
*/ 
public class T {  
  static final int threads = 100;  
  static final int NUMBER = 100;  
 
  public static void main(String[] args) throws Exception {  
    Map<String, Integer> hashmapSync = Collections.synchronizedMap(new HashMap<String, Integer>());  
    Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<String, Integer>();  
    Map<String, Integer> hashtable = new Hashtable<String, Integer>();  
 
    long totalA = 0;  
    long totalB = 0;  
    long totalC = 0;  
    for (int i = 0; i <= 10; i++) {  
      totalA += testPut(hashmapSync);  
      totalB += testPut(concurrentHashMap);  
      totalC += testPut(hashtable);  
    }  
    System.out.println("Put time HashMapSync=" + totalA + "ms.");  
    System.out.println("Put time ConcurrentHashMap="+ totalB + "ms.");  
    System.out.println("Put time Hashtable=" + totalC + "ms.");  
 
    totalA = 0;  
    totalB = 0;  
    totalC = 0;  
    for (int i = 0; i <= 10; i++) {  
      totalA += testGet(hashmapSync);  
      totalB += testGet(concurrentHashMap);  
      totalC += testGet(hashtable);  
    }  
    System.out.println("Get time HashMapSync=" + totalA + "ms.");  
    System.out.println("Get time ConcurrentHashMap="+ totalB + "ms.");  
    System.out.println("Get time Hashtable=" + totalC + "ms.");  
  }  
 
  public static long testPut(Map<String, Integer> map) throws Exception {  
    long start = System.currentTimeMillis();  
    for (int i = 0; i < threads; i++) {  
      new MapPutThread(map).start();  
    }  
    while (MapPutThread.counter > 0) {  
      Thread.sleep(1);  
    }  
    return System.currentTimeMillis() - start;  
  }  
 
  public static long testGet(Map<String, Integer> map) throws Exception {  
    long start = System.currentTimeMillis();  
    for (int i = 0; i < threads; i++) {  
      new MapPutThread(map).start();  
    }  
    while (MapPutThread.counter > 0) {  
      Thread.sleep(1);  
    }  
    return System.currentTimeMillis() - start;  
  }  
}  
 
class MapPutThread extends Thread {  
  static int counter = 0;  
  static Object lock = new Object();  
  private Map<String, Integer> map;  
  private String key = this.getId() + "";  
 
  MapPutThread(Map<String, Integer> map) {  
    synchronized (lock) {  
      counter++;  
    }  
    this.map = map;  
  }  
 
  public void run() {  
 
    for (int i = 1; i <= T.NUMBER; i++) {  
      map.put(key, i);  
    }  
    synchronized (lock) {  
      counter--;  
    }  
  }  
}  
 
class MapGetThread extends Thread {  
  static int counter = 0;  
  static Object lock = new Object();  
  private Map<String, Integer> map;  
  private String key = this.getId() + "";  
 
  MapGetThread(Map<String, Integer> map) {  
    synchronized (lock) {  
      counter++;  
    }  
    this.map = map;  
  }  
 
  public void run() {  
 
    for (int i = 1; i <= T.NUMBER; i++) {  
      map.get(key);  
    }  
    synchronized (lock) {  
      counter--;  
    }  
  }  

学习文档

http://www.ibm.com/developerworks/java/library/j-jtp08223/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值