现在的位置: 首页 > 编程语言 > 正文

JDK源代码分析聚集篇——-Collections的线程安全(穿上交通协管员的制服)

2019年06月11日 编程语言 ⁄ 共 3009字 ⁄ 字号 评论关闭
我们在对前面的几个聚集类进行分析的时候,我们发现,聚集类是一个curd很频繁的类,那么,他就不可避免的涉及到线程安全的问题,我们知道Vector是线程安全的,但是,其他的类,怎么能让他达到线程安全的地步呢,我们就不得不拉出Collections类来;其中有三个精彩的方法:

  1.   public static <T> Set<T> synchronizedSet(Set<T> s) {
  2.     return new SynchronizedSet<T>(s);
  3.     }
  1.  static class SynchronizedSet<E>
  2.       extends SynchronizedCollection<E>
  3.       implements Set<E> {
  4.     private static final long serialVersionUID = 487447009682186044L;
  5.     SynchronizedSet(Set<E> s) {
  6.             super(s);
  7.         }
  8.     SynchronizedSet(Set<E> s, Object mutex) {
  9.             super(s, mutex);
  10.         }
  11.     public boolean equals(Object o) {
  12.         synchronized(mutex) {return c.equals(o);}
  13.         }
  14.     public int hashCode() {
  15.         synchronized(mutex) {return c.hashCode();}
  16.         }
  17.     }
  1.     // use serialVersionUID from JDK 1.2.2 for interoperability
  2.     private static final long serialVersionUID = 3053995032091335093L;
  3.     final Collection<E> c;  // Backing Collection
  4.     final Object mutex;     // Object on which to synchronize
  5.     SynchronizedCollection(Collection<E> c) {
  6.             if (c==null)
  7.                 throw new NullPointerException();
  8.         this.c = c;
  9.             mutex = this;
  10.         }
  11.     SynchronizedCollection(Collection<E> c, Object mutex) {
  12.         this.c = c;
  13.             this.mutex = mutex;
  14.         }
  15.     public int size() {
  16.         synchronized(mutex) {return c.size();}
  17.         }
  18.     public boolean isEmpty() {
  19.         synchronized(mutex) {return c.isEmpty();}
  20.         }
  21.     public boolean contains(Object o) {
  22.         synchronized(mutex) {return c.contains(o);}
  23.         }
  24.     public Object[] toArray() {
  25.         synchronized(mutex) {return c.toArray();}
  26.         }
  27.     public <T> T[] toArray(T[] a) {
  28.         synchronized(mutex) {return c.toArray(a);}
  29.         }
  30.     public Iterator<E> iterator() {
  31.             return c.iterator(); // Must be manually synched by user!
  32.         }
  33.     public boolean add(E e) {
  34.         synchronized(mutex) {return c.add(e);}
  35.         }
  36.     public boolean remove(Object o) {
  37.         synchronized(mutex) {return c.remove(o);}
  38.         }
  39.     public boolean containsAll(Collection<?> coll) {
  40.         synchronized(mutex) {return c.containsAll(coll);}
  41.         }
  42.     public boolean addAll(Collection<? extends E> coll) {
  43.         synchronized(mutex) {return c.addAll(coll);}
  44.         }
  45.     public boolean removeAll(Collection<?> coll) {
  46.         synchronized(mutex) {return c.removeAll(coll);}
  47.         }
  48.     public boolean retainAll(Collection<?> coll) {
  49.         synchronized(mutex) {return c.retainAll(coll);}
  50.         }
  51.     public void clear() {
  52.         synchronized(mutex) {c.clear();}
  53.         }
  54.     public String toString() {
  55.         synchronized(mutex) {return c.toString();}
  56.         }
  57.         private void writeObject(ObjectOutputStream s) throws IOException {
  58.         synchronized(mutex) {s.defaultWriteObject();}
  59.         }
  60.     }

List和Map方法同理,这样,我们利用了装实模式,给我们的Map和List穿上了交通协管员的制服,减少了类爆炸,这就是装实模式;

  1. package org;
  2. public class AImp implements IA {
  3.     public synchronized void say() {
  4.         ;
  5.     }
  6. }
  1. package org;
  2. public interface IA {
  3.  public void say();
  4. }

sychronized标识符是不影响接口的实现和继承的;

抱歉!评论已关闭.