对并发Map的测试

/**
 * ConcurrentHashMap效率最高
 */
public class MapTest {


    public static void main(String[] args) throws InterruptedException {
        pressureTest(new Hashtable<>(), 5);
        pressureTest(Collections.synchronizedMap(new HashMap<>()), 5);
        pressureTest(new ConcurrentHashMap<>(),5);

    }

    private static  void  pressureTest(Map<Integer,Integer> map , int threshold) throws InterruptedException {
        System.out.println("start pressure map  [ "+ map.getClass().getName() + " ] use thread size "+ threshold);

        long start = System.currentTimeMillis();
        for(int i = 0; i<5; i++){
            ExecutorService executorService = Executors.newFixedThreadPool(threshold);
            for (int j=0; j<threshold;j++){
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        for(int k=0;k<500000;k++){
                            int ceil = (int)Math.ceil(Math.random() * 600000);
                            map.put(ceil, ceil);
                        }
                    }
                });
            }
            executorService.shutdown();
            executorService.awaitTermination(1, TimeUnit.HOURS);
        }
        long end = System.currentTimeMillis();
        long average = (end-start)/5;
        System.out.println("for the map [ "+map.getClass().getName()+" ] , the average time is "+average +" ms");
    }
}
原文地址:https://www.cnblogs.com/moris5013/p/12071122.html