ConcurrentHashMap相比hashtable--极端情况性能提高

测试极端情况下百万次修改性能情况

  • 开启了16个线程,初始化ConcurrentHashMap分为16个字段
  • 同时修改ConcurrentHashMap 和HashTable

import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author WuYe
 * @vesion 1.0 2019/11/4
 * /
 * /**
 * @program: conCurrentProject
 * @description: conCurrentHashMap  比hsahtable有8-9倍的效率提高分段锁原理
 * @author: WuYe
 * @create: 2019-11-04 21:02
 **/
public class syncMap {
      private  static ConcurrentHashMap<Integer,Integer> map1=new ConcurrentHashMap(16,0.9f,16);
      private  static Hashtable<Integer,Integer> map2= new Hashtable<Integer, Integer>(16,0.9f);
   private static Object object = new Object();
    public static void main(String[] args) {
        

        int a=0;
        while (a<16){
            new myThread(a+" of hashTable",a,map2).start();
            a++;
        }
        a=0;
        while (a<16){
            new myThread(a+" of ConcurrentMap",a,map1).start();
            a++;
        }
    }
    static class myThread extends Thread{
        private  int i;
        private  Map map;
        ThreadLocal<Integer> lid=new ThreadLocal(){
            @Override
            public Integer initialValue(){
                return i;
            }
        };
        public myThread(String name,int i,Map map){
            super(name);
            this.i=i;
            this.map=map;
        }
        @Override
        public void run(){
            int i = 0;
                    System.out.println(Thread.currentThread().getName()+"---start--put-->"+lid.get());
                    Long timeStart=System.currentTimeMillis();
            while ( i<100000000 ) {
                           map.put(lid.get(),lid.get());
                           i++;
            }
                    Long timeEnd=System.currentTimeMillis();
                    System.out.println("thread---"+Thread.currentThread().getName()+"over--put-->"+lid.get()+
                            "useTime---"+(timeEnd-timeStart));
        }
    }
}

修改次数更多,差异会更大时间检测

  • hashmapTime—2484868000—hashTable-4385328600—beishu—1.7648135031719996`
  • 在java并发编程–》用了闭锁来确认线程一起开始,最后一个线程结束时停止计时
package com.ihep.wuye;

import java.util.concurrent.CountDownLatch;

/**
 * @author WuYe
 * @vesion 1.0 2019/11/5
 * /
 * /**
 * @program: conCurrentProject
 * @description:
 * @author: WuYe
 * @create: 2019-11-05 22:06
 **/
public class runTime {

        public static long timeTasks(int nThreads, final Runnable task) throws InterruptedException
        {
            final CountDownLatch startGate = new CountDownLatch(1);
            final CountDownLatch endGate = new CountDownLatch(nThreads);
            for (int i = 0; i < nThreads; i++)
            {
                Thread t = new Thread()
                {
                        @Override
                        public void run()
                    {
                        try{
                                startGate.await();
                                try {
                                    task.run();
                                }
                                finally {
                                    endGate.countDown();
                                }
                            }
                    catch(InterruptedException ignored) {}
                    }
                };
                t.start();
            }
            long start = System.nanoTime();
            startGate.countDown();
            endGate.await();
            long end = System.nanoTime();
            return end - start;
        }
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值