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();}
            }
        }

  • 相关阅读:
    linux crontab 常用时间设置
    远程连接 mysql 数据库连接不上的解决方案
    Caffe搭建:Ubuntu14.04 + CUDA7.0 + opencv3.0 + Matlab2014A
    Ubuntu中sublime和Foxit Reader不能使用中文输入法解决方案
    python学习:python的星号(*)和双星号(**)用法
    Latex使用:在latex中添加算法模块
    Torch,Tensorflow使用: Ubuntu14.04(x64)+ CUDA8.0 安装 Torch和Tensorflow
    Caffe使用: Ubuntu 14.04(x64) 从cuda 7.0 升级到 cuda8.0
    Git使用:Linux(Ubuntu 14.04 x64)下安装Git并配置连接GitHub
    Git使用:安装,使用及常用命令整理
  • 原文地址:https://www.cnblogs.com/vigarbuaa/p/2848284.html
Copyright © 2011-2022 走看看