ArrayList,LinkedList都是线程不安全的类,可以通过Collections的synchronizedList方法返回一个线程安全类。
使用案例:
List list = Collections.synchronizedList(new ArrayList());
实现原理:
使用代理模式,Collections的synchronizedList方法返回一个新的SynchronizedList实例,该对象代理所有List中方法,所有方法上都加上了同步。
SynchronizedList类:
static class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> { static final long serialVersionUID = -7754090372962971524L; final List<E> list; SynchronizedList(List<E> list) { super(list); this.list = list; } SynchronizedList(List<E> list, Object mutex) { super(list, mutex); this.list = list; } public boolean equals(Object o) { synchronized(mutex) {return list.equals(o);} } public int hashCode() { synchronized(mutex) {return list.hashCode();} } public E get(int index) { synchronized(mutex) {return list.get(index);} } public E set(int index, E element) { synchronized(mutex) {return list.set(index, element);} } public void add(int index, E element) { synchronized(mutex) {list.add(index, element);} } public E remove(int index) { synchronized(mutex) {return list.remove(index);} } public int indexOf(Object o) { synchronized(mutex) {return list.indexOf(o);} } public int lastIndexOf(Object o) { synchronized(mutex) {return list.lastIndexOf(o);} } public boolean addAll(int index, Collection<? extends E> c) { synchronized(mutex) {return list.addAll(index, c);} } public ListIterator<E> listIterator() { return list.listIterator(); // Must be manually synched by user } public ListIterator<E> listIterator(int index) { return list.listIterator(index); // Must be manually synched by user } public List<E> subList(int fromIndex, int toIndex) { synchronized(mutex) { return new SynchronizedList<E>(list.subList(fromIndex, toIndex), mutex); } }
Collections内部类SynchronizedCollection:
static class SynchronizedCollection<E> implements Collection<E>, Serializable { // use serialVersionUID from JDK 1.2.2 for interoperability private static final long serialVersionUID = 3053995032091335093L; final Collection<E> c; // Backing Collection final Object mutex; // Object on which to synchronize,同步锁,注意不是this上加锁 SynchronizedCollection(Collection<E> c) { if (c==null) throw new NullPointerException(); this.c = c; mutex = this; } SynchronizedCollection(Collection<E> c, Object mutex) { this.c = c; this.mutex = mutex; } public int size() { synchronized(mutex) {return c.size();} } public boolean isEmpty() { synchronized(mutex) {return c.isEmpty();} } public boolean contains(Object o) { synchronized(mutex) {return c.contains(o);} } public Object[] toArray() { synchronized(mutex) {return c.toArray();} } public <T> T[] toArray(T[] a) { synchronized(mutex) {return c.toArray(a);} } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { synchronized(mutex) {return c.add(e);} } public boolean remove(Object o) { synchronized(mutex) {return c.remove(o);} } public boolean containsAll(Collection<?> coll) { synchronized(mutex) {return c.containsAll(coll);} } public boolean addAll(Collection<? extends E> coll) { synchronized(mutex) {return c.addAll(coll);} } public boolean removeAll(Collection<?> coll) { synchronized(mutex) {return c.removeAll(coll);} } public boolean retainAll(Collection<?> coll) { synchronized(mutex) {return c.retainAll(coll);} } public void clear() { synchronized(mutex) {c.clear();} } public String toString() { synchronized(mutex) {return c.toString();} } private void writeObject(ObjectOutputStream s) throws IOException { synchronized(mutex) {s.defaultWriteObject();} }