hashMap,你需要了解的HashMap、HashTable和ConcurrentHashMap的區別

 2023-10-06 阅读 23 评论 0

摘要:文章目錄三者的簡介HashMapHashTableConcurrentHashMap 三者的簡介 hashMap。面試中經常被問到的一個問題,HashMap和ConcurrentHashMap區別是什么,今天把這個問題好好整理一下。 HashMap是線程不安全的,當出現多線程操作時,會出現安全隱患࿰

文章目錄

  • 三者的簡介
  • HashMap
  • HashTable
  • ConcurrentHashMap

三者的簡介

hashMap。面試中經常被問到的一個問題,HashMap和ConcurrentHashMap區別是什么,今天把這個問題好好整理一下。
HashMap是線程不安全的,當出現多線程操作時,會出現安全隱患,我們可能會想到HashTable,是的,這個是線程安全的,但是HashTable用的是方法鎖,把整個put方法都上鎖了,這就導致了效率很低,如果把put方法比作是一個有很多房間的院子,那么HathTable的鎖就相當于是把院子的大門鎖上了。而ConcurrentHashMap是用的塊鎖,相當于是把院子里的有安全隱患的房間鎖上了,這樣一來,就不會讓去其他房間辦事的人等待了。

HashMap

HashMap是線程不安全的,在原碼中對put方法沒有做鎖的處理,當放生多線程時,會有線程安全問題,下面通過一個簡單的例子進行演示,創建三個線程,并且啟動,在run方法里通過for循環給map存100個值,然后輸出map的大小按正常來說,該map的大小應該是100,而這里輸出了176。

class Demo implements Runnable{static Map<String,String> map = new HashMap<>();@Overridepublic 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());}
}

在這里插入圖片描述

HashTable

Hashtable。HashTable用到了鎖,而且是直接給put方法加的鎖,線程肯定是安全的了,這里我們在測試線程安全的同時,看一下執行時間,這里通過put10000個數據進行測試,通過結果可以看到,map的大小確實是10000,而時間用了16ms左右。
在這里插入圖片描述

class Demo implements Runnable{static Map<String,String> map = new Hashtable<>();@Overridepublic 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());}
}

在這里插入圖片描述

ConcurrentHashMap

ConcurrentHashMap用的是塊鎖,哪塊不安全就鎖哪塊,不能不鎖,不能全鎖,那我就塊鎖!看看這個塊鎖相對于方法鎖是快了,還是慢了。
在這里插入圖片描述

class Demo implements Runnable{static Map<String,String> map = new ConcurrentHashMap<>();@Overridepublic 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());}
}

在這里插入圖片描述
從結果中看到,從之前的20ms和22ms提高到了現在的17ms和18ms

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/122863.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息