说说线程安全包装: Collections.synchronizedList
说说线程安全包装: Collections.synchronizedList
java 集合工具类 Collections.synchronizedList 提供了集合的线程安全包装方法。
那么它是如何让一个集合变成线程安全的呢?为什么说这种线程安全集合的实现效率非常低下?
synchronizedList 的实现
我们来看源码吧。
public static <T> List<T> synchronizedList(List<T> list) {
return (list instanceof RandomAccess ?
new SynchronizedRandomAccessList<>(list) :
new SynchronizedList<>(list));
}
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();
}
public ListIterator<E> listIterator(int index) {
return list.listIterator(index);
}
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);}
}
private Object readResolve() {
return (list instanceof RandomAccess
? new SynchronizedRandomAccessList<>(list)
: this);
}
}
当 synchronizedList 传入的参数类型是 ArrayList 时, 因为 ArrayList 实现了 RandomAccess 接口,所以 synchronizedList 会构建一个 SynchronizedRandomAccessList 对象,不过没关系,SynchronizedList 是 SynchronizedRandomAccessList 的父类,我们直接看他的实现。
list 对象直接维护了传递进来的参数 List 类型参数
而在 get set add remove 等方法中的实现都用线程同步语句块 synchronized (mutex)
封装起来。那么 mutex 这把锁是谁呢? 看起来要到 super(list); 里面去找了. 这时候就来到了 SynchronizedList 的父类 SynchronizedCollection
static class SynchronizedCollection<E> implements Collection<E>, Serializable {
private static final long serialVersionUID = 3053995032091335093L;
final Collection<E> c;
final Object mutex;
SynchronizedCollection(Collection<E> c) {
this.c = Objects.requireNonNull(c);
mutex = this;
}
SynchronizedCollection(Collection<E> c, Object mutex) {
this.c = Objects.requireNonNull(c);
this.mutex = Objects.requireNonNull(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();
}
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();}
}
@Override
public void forEach(Consumer<? super E> consumer) {
synchronized (mutex) {c.forEach(consumer);}
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
synchronized (mutex) {return c.removeIf(filter);}
}
@Override
public Spliterator<E> spliterator() {
return c.spliterator();
}
@Override
public Stream<E> stream() {
return c.stream();
}
@Override
public Stream<E> parallelStream() {
return c.parallelStream();
}
private void writeObject(ObjectOutputStream s) throws IOException {
synchronized (mutex) {s.defaultWriteObject();}
}
}
我们清楚的看到 mutex = this; 这个锁就是对象自己!
通过上面的源码我们可以知道了, synchronizedList 实现线程安全的方法就是对自己暴力加锁,这效率能不低下吗?
在获取安全的 list 后遍历时,外层为何还要用 synchronized 同步?
官方文档就是如此使用 synchronizedList 的:
List list = Collections.synchronizedList(new ArrayList());
...
synchronized (list) {
Iterator i = list.iterator();
while (i.hasNext())
foo(i.next());
}
问题来了: 既然封装类内部已经加了对象锁,为什么外部还要加一层对象锁?
先看下官方文档的解释吧:
# Understanding Collections and Thread Safety in Java
NOTE:
When using the iterator of a synchronized collection, we should use synchronized block to safeguard the iteration code because the iterator itself is not thread-safe. Consider the following code:
List<String> safeList = Collections.synchronizedList(new ArrayList<>());
Iterator<String> iterator = safeList.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
System.out.println(next);
}
Although the safeList is thread-safe, its iterator is not, so we should manually add synchronized block like this:
synchronized (safeList) {
while (iterator.hasNext()) {
String next = iterator.next();
System.out.println(next);
}
}
其实, 这个迭代的外部锁其实就是为了 list.iterator() 在读取过程中,不会本来 hasNext() 有的,但在调用 i.next() 的时候,另外一个线程把它删了,这个 synchronized 块是为了保障这三行代码在多个线程里同时执行的并发问题。
至于 synchronizedList 的内部锁,那是在并发执行 add/remove 的时候,不要把多个线程的东西加到 list 内部实现的同一个位置上去,导致数据丢失或者脏数据等问题,这是为了保证这个 List 在执行 add/remove 时不会存在并发问题。
简而言之,这两个锁是不同层面上的并发问题。
所以,当我们对 synchronizedList 进行遍历的时候一定不要忘了,在外部也加上 synchronized(list),以保证线程安全。