zoukankan      html  css  js  c++  java
  • 转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();}
            }
        }

  • 相关阅读:
    使用Visual Studio给SharePoint列表添加Event Receiver
    使用客户端对象模型回写SharePoint列表
    思考:通过源码进行安装的前提是什么
    思考:学习一门语言真的是仅仅只有为了使用吗?或者说学习一个语言如果在工作中不使用,那么学习它还有什么好处和价值呢?
    思考:(使用下载工具比如yum,maven等)下载(库或者jar)到本地都需要设置源
    思考:代码块的大小是进行代码抽出一个方法或者进行重构或者是否在设计时设计为一个方法的衡量因素之一吗?
    思考:对源的包装是为了更好的隔离和在中间插入一些层?
    Spring Cloud和Spring Boot的认识
    如何对待基础知识:
    为什么总觉得事情应付不过来,总觉得事情需要花费很多时间?
  • 原文地址:https://www.cnblogs.com/vigarbuaa/p/2848284.html
Copyright © 2011-2022 走看看