Java中Map實現線程安全的3種方式
方式1. 使用Hashtable
Map<String,Object> hashtable=new Hashtable<String,Object>();
這是所有人最先想到的,那為什么它是線程安全的?那就看看它的源碼,我們可以看出我們常用的put,get,containsKey等方法都是同步的,所以它是線程安全的
public synchronized boolean containsKey(Object key) { ? ? ? ? Entry<?,?> tab[] = table; ? ? ? ? int hash = key.hashCode(); ? ? ? ? int index = (hash & 0x7FFFFFFF) % tab.length; ? ? ? ? for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { ? ? ? ? ? ? if ((e.hash == hash) && e.key.equals(key)) { ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return false; ? ? } ?public synchronized V get(Object key) { ? ? ? ? Entry<?,?> tab[] = table; ? ? ? ? int hash = key.hashCode(); ? ? ? ? int index = (hash & 0x7FFFFFFF) % tab.length; ? ? ? ? for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { ? ? ? ? ? ? if ((e.hash == hash) && e.key.equals(key)) { ? ? ? ? ? ? ? ? return (V)e.value; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } ? ? ?public synchronized V put(K key, V value) { ? ? ? ? // Make sure the value is not null ? ? ? ? if (value == null) { ? ? ? ? ? ? throw new NullPointerException(); ? ? ? ? } ? ? ? ? // Makes sure the key is not already in the hashtable. ? ? ? ? Entry<?,?> tab[] = table; ? ? ? ? int hash = key.hashCode(); ? ? ? ? int index = (hash & 0x7FFFFFFF) % tab.length; ? ? ? ? @SuppressWarnings("unchecked") ? ? ? ? Entry<K,V> entry = (Entry<K,V>)tab[index]; ? ? ? ? for(; entry != null ; entry = entry.next) { ? ? ? ? ? ? if ((entry.hash == hash) && entry.key.equals(key)) { ? ? ? ? ? ? ? ? V old = entry.value; ? ? ? ? ? ? ? ? entry.value = value; ? ? ? ? ? ? ? ? return old; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? addEntry(hash, key, value, index); ? ? ? ? return null; ? ? }
其實現原理是在增刪改查的方法上使用了synchronized鎖機制,在多線程環(huán)境下,無論是讀數據,還是修改數據,在同一時刻只能有一個線程在執(zhí)行synchronize方法,因為是對整個表進行鎖定。所以線程越多,對該map的競爭越激烈,效率越低,不推薦使用。
方式2. 使用Collections.synchronizedMap(new Hashtable())
其實現原理是使用工具類里面的靜態(tài)方法,把傳入進來的Hashtable包裝成同步的,即在增刪改查的方法上增加了synchronized所機制,其實現方式與Hashtable差不多,效率也差不多,不推薦使用。
Map map = Collections.synchronizedMap(new Hashtable());
以下是JDK源碼
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) { return new SynchronizedMap<>(m); } private static class SynchronizedMap<K,V> implements Map<K,V>, Serializable { private static final long serialVersionUID = 1978198479659022715L; private final Map<K,V> m; // Backing Map final Object mutex; // Object on which to synchronize SynchronizedMap(Map<K,V> m) { this.m = Objects.requireNonNull(m); mutex = this; } SynchronizedMap(Map<K,V> m, Object mutex) { this.m = m; this.mutex = mutex; } public int size() { synchronized (mutex) {return m.size();} } public boolean isEmpty() { synchronized (mutex) {return m.isEmpty();} } public boolean containsKey(Object key) { synchronized (mutex) {return m.containsKey(key);} } public boolean containsValue(Object value) { synchronized (mutex) {return m.containsValue(value);} } public V get(Object key) { synchronized (mutex) {return m.get(key);} } public V put(K key, V value) { synchronized (mutex) {return m.put(key, value);} } public V remove(Object key) { synchronized (mutex) {return m.remove(key);} } public void putAll(Map<? extends K, ? extends V> map) { synchronized (mutex) {m.putAll(map);} } public void clear() { synchronized (mutex) {m.clear();} } ...... }
方式3. 使用ConcurrentHashMap
其實現原理是Hashtable是對整個表進行加鎖,而ConcurrentHashMap是把表進行分段,初始情況下分成16段,每一段都有一把鎖,當多個線程訪問不同的段時,因為獲取到的鎖是不同的,所以可以并行的訪問。效率比Hashtable高多了,推薦使用。
到此這篇關于Java中Map實現線程安全的3種方式的文章就介紹到這了,更多相關Java Map線程安全內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
BeanUtils.copyProperties擴展--實現String轉Date
這篇文章主要介紹了BeanUtils.copyProperties擴展--實現String轉Date操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06javafx tableview鼠標觸發(fā)更新屬性詳解
這篇文章主要為大家詳細介紹了javafx tableview鼠標觸發(fā)更新屬性的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08springboot restTemplate連接池整合方式
這篇文章主要介紹了springboot restTemplate連接池整合方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10