Collections.synchronizedList与CopyOnWriteArrayList
Collections.synchronizedList和CopyOnWriteArrayList在并发场景下都可以使用,Collections.synchronizedList是Collections下的匿名内部类,而CopyOnWriteArrayList是juc包下的。
CopyOnWriteArrayList
推荐使用,juc包下的都是为并发而生,可以从字面意思就知道CopyOnWriteArrayList是每次写的时候都会拷贝一份进行操作,是不是就是读写分离。
add(E e) 源码
public class CopyOnWriteArrayList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { private static final long serialVersionUID = 8673264195747942595L; /** The lock protecting all mutators */ final transient ReentrantLock lock = new ReentrantLock(); /** The array, accessed only via getArray/setArray. */ private transient volatile Object[] array; /** * Gets the array. Non-private so as to also be accessible * from CopyOnWriteArraySet class. */ final Object[] getArray() { return array; } /** * Sets the array. */ final void setArray(Object[] a) { array = a; } /** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ public boolean add(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; Object[] newElements = Arrays.copyOf(elements, len + 1); newElements[len] = e; setArray(newElements); return true; } finally { lock.unlock(); } }
可以看到使用的ReentrantLock进行加锁操作,调用getArray获取当前对象的属性Object[] array,且使用Arrays.copyOf拷贝一个新的数组,将当前数据加入新数组中,并且重新赋值到array,最后释放锁。
如果当前读操作正好有写操作,写的时候是对副本进行操作,而读的是当前对象,互不影响
/** * {@inheritDoc} * * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { return get(getArray(), index); }
CopyOnWriteArrayList就是写的时候对拷贝对象进行操作,这样一来读写分离保证了List的一致性。
Collections.synchronizedList
Collections中的一个静态内部类,传入一个List,它可以将一个非安全的List转换成一个安全的List,内部使用synchronized对操作进行加锁。
public static <T> List<T> synchronizedList(List<T> list) { return (list instanceof RandomAccess ? new SynchronizedRandomAccessList<>(list) : new SynchronizedList<>(list)); }
当传入一个list对象是会进行判断是否RandomAccess,根据true或false执行不同的构造函数。 有意思的是SynchronizedRandomAccessList继承于SynchronizedList。
继承关系
SynchronizedList源码
/** * @serial include */ static class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> { private 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) { if (this == o) return true; 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<>(list.subList(fromIndex, toIndex), mutex); } } @Override public void replaceAll(UnaryOperator<E> operator) { synchronized (mutex) {list.replaceAll(operator);} } @Override public void sort(Comparator<? super E> c) { synchronized (mutex) {list.sort(c);} } /** * SynchronizedRandomAccessList instances are serialized as * SynchronizedList instances to allow them to be deserialized * in pre-1.4 JREs (which do not have SynchronizedRandomAccessList). * This method inverts the transformation. As a beneficial * side-effect, it also grafts the RandomAccess marker onto * SynchronizedList instances that were serialized in pre-1.4 JREs. * * Note: Unfortunately, SynchronizedRandomAccessList instances * serialized in 1.4.1 and deserialized in 1.4 will become * SynchronizedList instances, as this method was missing in 1.4. */ private Object readResolve() { return (list instanceof RandomAccess ? new SynchronizedRandomAccessList<>(list) : this); } }
内部使用synchronized来进行加锁,而锁的对象mutex就是当前对象this。在其父类SynchronizedCollection中定义的。
/** * @serial include */ static class SynchronizedCollection<E> implements Collection<E>, Serializable { private static final long serialVersionUID = 3053995032091335093L; final Collection<E> c; // Backing Collection final Object mutex; // Object on which to synchronize
值的注意的是在SynchronizedList中存在两个没有加锁的方法,需要用户手动加锁的,如下:
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 }
在SynchronizedCollection中也存在没加锁的方法,迭代器,分割,流操作都需要手动加锁!!
/** * @serial include */ static class SynchronizedCollection<E> implements Collection<E>, Serializable { public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } @Override public Spliterator<E> spliterator() { return c.spliterator(); // Must be manually synched by user! } @Override public Stream<E> stream() { return c.stream(); // Must be manually synched by user! } @Override public Stream<E> parallelStream() { return c.parallelStream(); // Must be manually synched by user! } }
在Collections类中,其实就是利用了同步代码块来保证数据的安全,而有几个方法需要手动进行加锁处理。