首頁 > Java > Java基礎 > 主體

hashmap和concurrenthashmap的差別是什麼

青灯夜游
發布: 2021-02-02 11:14:18
原創
12535 人瀏覽過

區別:HashMap是線程不安全的,當出現多線程操作時,會出現安全隱患;而ConcurrentHashMap是線程安全的。 HashMap不支援並發操作,沒有同步方法;ConcurrentHashMap支援並發操作。

hashmap和concurrenthashmap的差別是什麼

本教學操作環境:windows7系統、java10版,DELL G3電腦。

hashmap與concurrenthashmap的差異

  • HashMap是執行緒不安全的,當出現多執行緒操作時,會出現安全隱患;而ConcurrentHashMap是線程安全的。

  • HashMap不支援並發操作,沒有同步方法,ConcurrentHashMap支援並發操作,透過繼承ReentrantLock(JDK1.7重入鎖定)/CAS和synchronized(JDK1.8內建鎖定)來進行加鎖(分段鎖),每次需要加鎖的操作鎖住的是一個segment,這樣只要確保每個Segment 是線程安全的,也就實現了全局的線程安全。

ConcurrentHashMap採用鎖定分段技術,將整個Hash桶進行了分段segment,也就是將這個大的數組分成了幾個小的片段segment,而且每個小的片段segment上面都有鎖存在,那麼在插入元素的時候就需要先找到應該插入到哪一個片段segment,然後再在這個片段上面進行插入,而且這裡還需要獲取segment鎖。

ConcurrentHashMap讓鎖定的粒度更精細一些,並發效能更好。

(推薦教學:java入門教學

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());
    }
}
登入後複製

hashmap和concurrenthashmap的差別是什麼

HashTable

HashTable用到了鎖,而且是直接給put方法加的鎖,線程肯定是安全的了,這裡我們在測試線程安全的同時,看一下執行時間,這裡透過put10000個資料進行測試,透過結果可以看到,map的大小確實是10000,而時間用了16ms左右。

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());
    }
}
登入後複製

hashmap和concurrenthashmap的差別是什麼

ConcurrentHashMap

ConcurrentHashMap用的是分段鎖,哪一塊不安全就鎖哪塊,不能不鎖,不能全鎖,那我就塊鎖!看看這個塊鎖相對於方法鎖是快了,還是慢了。

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());
    }
}
登入後複製

hashmap和concurrenthashmap的差別是什麼

從結果中看到,從先前的20ms和22ms提高到了現在的17ms和18ms

#更多電腦程式相關知識,請造訪:程式設計影片! !

以上是hashmap和concurrenthashmap的差別是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!