java中hashmap和concurrenthashmap的区别有哪些

发布时间:2021-02-03 14:07:50 作者:小新
来源:亿速云 阅读:319

小编给大家分享一下java中hashmap和concurrenthashmap的区别有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

区别:HashMap是线程不安全的,当出现多线程操作时,会出现安全隐患;而ConcurrentHashMap是线程安全的。HashMap不支持并发操作,没有同步方法;ConcurrentHashMap支持并发操作。

hashmap和concurrenthashmap的区别

ConcurrentHashMap采用锁分段技术,将整个Hash桶进行了分段segment,也就是将这个大的数组分成了几个小的片段segment,而且每个小的片段segment上面都有锁存在,那么在插入元素的时候就需要先找到应该插入到哪一个片段segment,然后再在这个片段上面进行插入,而且这里还需要获取segment锁。

ConcurrentHashMap让锁的粒度更精细一些,并发性能更好。

HashMap

HashMap是线程不安全的,在原码中对put方法没有做锁的处理,当放生多线程时,会有线程安全问题,下面通过一个简单的例子进行演示,创建三个线程,并且启动,在run方法里通过for循环给map存100个值,然后输出map的大小按正常来说,该map的大小应该是100,而这里输出了176。

class Demo implements Runnable{
    static Map<String,String> map = new HashMap<>();

    @Override
    public void run() {
        for (int i = 0; i < 100; i ++) {
            map.put(i + "","value");
        }
    }

    public static void main(String[] args) {

        new Thread(new Demo()).start();
        new Thread(new Demo()).start();
        new Thread(new Demo()).start();
        // 获取当前线程
        Thread currentThread = Thread.currentThread();
        // 当前线程睡眠2秒,让上面的三个线程先执行
        try {
            currentThread.sleep(2000);
        } catch (Exception e) {
            e.getMessage();
        }
        // 上面的线程执行完毕后输出map的大小
        System.out.println(map.size());
    }
}

java中hashmap和concurrenthashmap的区别有哪些

HashTable

HashTable用到了锁,而且是直接给put方法加的锁,线程肯定是安全的了,这里我们在测试线程安全的同时,看一下执行时间,这里通过put10000个数据进行测试,通过结果可以看到,map的大小确实是10000,而时间用了16ms左右。

java中hashmap和concurrenthashmap的区别有哪些

class Demo implements Runnable{
    static Map<String,String> map = new Hashtable<>();

    @Override
    public void run() {
        long startTime = System.currentTimeMillis(); //获取开始时间
        for (int i = 0; i < 10000; i ++) {
            map.put(i + "","value");
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println((endTime - startTime) + "ms");
    }

    public static void main(String[] args) {

        new Thread(new Demo()).start();
        new Thread(new Demo()).start();
        new Thread(new Demo()).start();
        // 获取当前线程
        Thread currentThread = Thread.currentThread();
        // 当前线程睡眠2秒,让上面的三个线程先执行
        try {
            currentThread.sleep(2000);
        } catch (Exception e) {
            e.getMessage();
        }
        // 上面的线程执行完毕后输出map的大小
        System.out.println(map.size());
    }
}

java中hashmap和concurrenthashmap的区别有哪些

ConcurrentHashMap

ConcurrentHashMap用的是分段锁,哪块不安全就锁哪块,不能不锁,不能全锁,那我就块锁!看看这个块锁相对于方法锁是快了,还是慢了。

java中hashmap和concurrenthashmap的区别有哪些

class Demo implements Runnable{
    static Map<String,String> map = new ConcurrentHashMap<>();

    @Override
    public void run() {
        long startTime = System.currentTimeMillis(); //获取开始时间
        for (int i = 0; i < 10000; i ++) {
            map.put(i + "","value");
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println((endTime - startTime) + "ms");
    }

    public static void main(String[] args) {

        new Thread(new Demo()).start();
        new Thread(new Demo()).start();
        new Thread(new Demo()).start();
        // 获取当前线程
        Thread currentThread = Thread.currentThread();
        // 当前线程睡眠2秒,让上面的三个线程先执行
        try {
            currentThread.sleep(2000);
        } catch (Exception e) {
            e.getMessage();
        }
        // 上面的线程执行完毕后输出map的大小
        System.out.println(map.size());
    }
}

java中hashmap和concurrenthashmap的区别有哪些

从结果中看到,从之前的20ms和22ms提高到了现在的17ms和18ms

看完了这篇文章,相信你对“java中hashmap和concurrenthashmap的区别有哪些”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. java中HashMap和Hashtable之间的区别有哪些
  2. Unicode和UTF-8区别有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java hashmap concurrenthashmap

上一篇:position函数怎么在postgresql 中使用

下一篇:微信小程序和支付宝小程序的区别是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》