比较SynchronizedMap、Hashtable和ConcurrentHashMap的效率

分别通过三种方式创建Map对象,使用ExecutorService来并发运行5个线程,每个线程添加/获取500K个元素。

package cglib;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

 

public class StringNumber {
    
    
    public final static int THREAD_POOL_SIZE = 5;
    
    public static Map<String, Integer> crunchifyHashTableObject = null;
    public static Map<String, Integer> crunchifySynchronizedMapObject = null;
    public static Map<String, Integer> crunchifyConcurrentHashMapObject = null;
 
    public static void main(String[] args) throws InterruptedException {
 
        // Test with Hashtable Object
        crunchifyHashTableObject = new Hashtable<>();
        crunchifyPerformTest(crunchifyHashTableObject);
 
        // Test with synchronizedMap Object
        crunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());
        crunchifyPerformTest(crunchifySynchronizedMapObject);
 
        // Test with ConcurrentHashMap Object
        crunchifyConcurrentHashMapObject = new ConcurrentHashMap<>();
        crunchifyPerformTest(crunchifyConcurrentHashMapObject);
 
    }
 
    public static void crunchifyPerformTest(final Map<String, Integer> crunchifyThreads) throws InterruptedException {
 
        System.out.println("Test started for: " + crunchifyThreads.getClass());
        long averageTime = 0;
        for (int i = 0; i < 5; i++) {
 
            long startTime = System.nanoTime();
            ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
 
            for (int j = 0; j < THREAD_POOL_SIZE; j++) {
                crunchifyExServer.execute(new Runnable() {
                    @SuppressWarnings("unused")
                    @Override
                    public void run() {
 
                        for (int i = 0; i < 500000; i++) {
                            Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000);
 
                            // Retrieve value. We are not using it anywhere
                            Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));
 
                            // Put value
                            crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
                        }
                    }
                });
            }
 
            // Make sure executor stops
            crunchifyExServer.shutdown();
 
            // Blocks until all tasks have completed execution after a shutdown request
            crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
 
            long entTime = System.nanoTime();
            long totalTime = (entTime - startTime) / 1000000L;
            averageTime += totalTime;
            System.out.println("2500K entried added/retrieved in " + totalTime + " ms");
        }
        System.out.println("For " + crunchifyThreads.getClass() + " the average time is " + averageTime / 5 + " ms\n");
    }
    
}
      


输出:

Test started for: class java.util.Hashtable
2500K entried added/retrieved in 3816 ms
2500K entried added/retrieved in 2916 ms
2500K entried added/retrieved in 3040 ms
2500K entried added/retrieved in 2819 ms
2500K entried added/retrieved in 2855 ms
For class java.util.Hashtable the average time is 3089 ms

Test started for: class java.util.Collections$SynchronizedMap
2500K entried added/retrieved in 3430 ms
2500K entried added/retrieved in 2775 ms
2500K entried added/retrieved in 2891 ms
2500K entried added/retrieved in 2710 ms
2500K entried added/retrieved in 2811 ms
For class java.util.Collections$SynchronizedMap the average time is 2923 ms

Test started for: class java.util.concurrent.ConcurrentHashMap
2500K entried added/retrieved in 1814 ms
2500K entried added/retrieved in 1135 ms
2500K entried added/retrieved in 1051 ms
2500K entried added/retrieved in 1008 ms
2500K entried added/retrieved in 1560 ms
For class java.util.concurrent.ConcurrentHashMap the average time is 1313 ms

 

 

转载于:https://my.oschina.net/u/2822116/blog/785674

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值