当前位置:编程学习 > 网站相关 >>

集合之线程安全

Map map = Collections.synchronizedMap(new HashMap(1));
                     Collections.synchronizedList(new ArrayList());
[java] view plaincopy
   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);} 
       }  www.zzzyk.com
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) { 
           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();} 
       } 
   } 
 
 
使用了Collections.synchronizedMap(new HashMap(1))就将非线程安全的变为线程安全的了,同时性能也降低了,但有些时候也并非那么完美,比如并发的时候:
[java] view plaincopy
Map map = Collections.synchronizedMap(new HashMap(1)); 
if(map.containsKey("str")){ 
 map.remove("str"); 

 
jdk1.5新增了ConcurrentLinkedQueue、ConcurrentHashMap、CopyOnWriteArrayList 和CopyOnWriteArraySet对这些集合进行并发修改是安全的。
补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,