Java之通過Collections.synchronizedMap創(chuàng)建線程安全的HashMap

1 問題

我們知道hashMap線程是不安全的,一般而言,我們怎么創(chuàng)建線程安全的HashMap呢?

 
2 解決辦法

我們可以使用Collections.synchronizedMap來創(chuàng)建HashMap,如下

static Map<String, String> results = Collections.synchronizedMap(new HashMap<String, String>());

 
3 Collections.synchronizedMap源碼部分實現(xiàn)

我們先看synchronizedMap如果創(chuàng)建

    public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
        return new SynchronizedMap<K,V>(m);
    }

我們看下具體的SynchronizedMap類的部分

    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 final Map<K,V> m;     // Backing Map
            final 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>>(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) {
                if (this == o)
                    return true;
                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();}
            }
        }

SynchronizedMap 實現(xiàn)了Map接口的代理類,該類中對Map接口中的方法還是使用synchronized 同步關(guān)鍵字來保證對Map的操作是線程安全的。

synchronized(mutex) {/*****/}


作者:chen.yu
深信服三年半工作經(jīng)驗,目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)