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);} } private Object readResolve() { return (list instanceof RandomAccess ? new SynchronizedRandomAccessList<>(list) : this); } }
官方文档就是下面的使用方式html
List list = Collections.synchronizedList(new ArrayList()); ... synchronized (list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); }
看源码可知,Collections.synchronizedList中不少方法,好比equals,hasCode,get,set,add,remove,indexOf,lastIndexOf......安全
都添加了锁,可是List中并发
Iterator<E> iterator();
这个方法没有加锁,不是线程安全的,因此若是要遍历,仍是必需要在外面加一层锁。dom
使用Iterator迭代器的话,彷佛也不必用Collections.synchronizedList的方法来包装了——反正都是必需要使用Synchronized代码块包起来的。ide
因此总的来讲,Collections.synchronizedList这种作法,适合不须要使用Iterator、对性能要求也不高的状况。性能
for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + ","); } Iterator iterator = list.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + ","); } for (Integer i : list) { System.out.print(i + ","); }
第一种是普通的for循环遍历、第二种是使用迭代器进行遍历,第三种咱们通常称之为加强for循环(for each)this
能够看到,第三种形式是JAVA提供的语法糖,这里咱们剖洗一下,这种加强for循环底层是如何实现的。.net
for (Integer i : list) { System.out.println(i); }
反编译后:线程
Integer i; for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i)){ i = (Integer)iterator.next(); }
若是在Vector,Collections.synchronizedList使用加强for循环,就必须在外面单独加锁,由于它不是单单一个操做,不是原子性的,若是在遍历的过程当中,进行add,remove操做,就会抛出异常。code
经过Collections.synchronizedList获取安全的list后,为什么还要用synchronized修饰?
【集合类型的并发】Collections.synchronizedList