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