转hashmap非线程安全的解决办法

HashTable为线程安全的Map对象,它是JDK 1.0的一部分。Hashtable提供了一种易用的、线程安全的、关联的map功能,然而,线程安全性是凭代价换来的——Hashtable的所有方法都是同步的,故现在的JDK不提倡使用HashTable。

Hashtable的后继者HashMap是作为JDK1.2中的集合框架的一部分出现的,它通过提供一个不同步的基类HashMap和一个同步的包装器Collections.synchronizedMap,解决了线程安全性问题。

通过将基本的功能从线程安全性中分离开来,Collections.synchronizedMap允许需要同步的用户可以拥有同步,而不需要同步的用户则不必为同步付出代价。

Map map = Collections.synchronizedMap(new HashMap());

一下内容为JDK中实现的Map同步代码

  private static class SynchronizedMap<K,V>
implements Map<K,V>, Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 1978198479659022715L;


private Map<K,V> m;     // Backing Map
        Object      mutex;// Object on which to synchronize


SynchronizedMap(Map<K,V> m) {
            if (m==null)
                throw new NullPointerException();
            this.m = 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();}
}


private transient Set<K> keySet = null;
private transient Set<Map.Entry<K,V>> entrySet = null;
private transient Collection<V> values = null;


public Set<K> keySet() {
            synchronized(mutex) {
                if (keySet==null)
                    keySet = new SynchronizedSet<K>(m.keySet(), mutex);
                return keySet;
            }
}


public Set<Map.Entry<K,V>> entrySet() {
            synchronized(mutex) {
                if (entrySet==null)
                    entrySet = new SynchronizedSet<Map.Entry<K,V>>((Set<Map.Entry<K,V>>)m.entrySet(), mutex);
                return entrySet;
            }
}


public Collection<V> values() {
            synchronized(mutex) {
                if (values==null)
                    values = new SynchronizedCollection<V>(m.values(), mutex);
                return values;
            }
        }


public boolean equals(Object o) {
            synchronized(mutex) {return m.equals(o);}
        }
public int hashCode() {
            synchronized(mutex) {return m.hashCode();}
        }
public String toString() {
    synchronized(mutex) {return m.toString();}
        }
        private void writeObject(ObjectOutputStream s) throws IOException {
    synchronized(mutex) {s.defaultWriteObject();}
        }
    }

原文地址:https://www.cnblogs.com/vigarbuaa/p/2848284.html