一、ArrayList 是不安全的
1、故障現象
public class NotSafeDemo { public static void main(String[] args) { List<String> list = new ArrayList(); for (int i = 0; i < 30; i++) { //多個線程同時對集合進行修改
new Thread(() -> { //向集合中添加內容
list.add(UUID.randomUUID().toString().substring(0, 8)); //從集合中獲取內容
System.out.println(list); }, String.valueOf(i)).start(); } } }
運行結果:
運行發生了異常,異常信息是:java.util.ConcurrentModificationException。
如果只有一個線程操作ArrayList,是沒有任何問題的。
2、分析原因
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! 這里會modCount++
elementData[size++] = e; return true; }
可以看到 add() 方法是沒有 synchronized,並不能保證線程安全。
在 ArrayList 的內部類 Itr 中這樣的一個方法:
final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }
這里判斷真正的 modCount 與 期望的 modCount 是否相等,如果不等就會拋出並發修改異常,就是為了防止多個線程對 ArrayList 的操作。
那么如何解決List 類型的線程安全問題呢?
二、方案一
1、使用 Vector
繼承結構:
Vector 是矢量隊列,它是 JDK1.0 版本添加的類。繼承於 AbstractList,實現了 List, RandomAccess, Cloneable 這些接口。 Vector 繼承了 AbstractList,實現了 List;所以, 它是一個隊列,支持相關的添加、刪除、修改、遍歷等功能。
Vector 實現了 RandmoAccess 接口,即提供了隨機訪問功能。RandmoAccess 是 java 中用來被 List 實現,為 List 提供快速訪問功能的。在Vector 中,我們即可以通過元素的序號快速獲取元素對象;這就是快速隨機訪問。 Vector 實現了 Cloneable 接口,即實現 clone()函數。它能被克隆。
和 ArrayList 不同, Vector 中的操作是線程安全的。
2、代碼實現
/** * List集合線程安全案例 */
public class SafeListDemo { public static void main(String[] args) { List<String> list = new Vector<>(); for (int i = 0; i < 30; i++) { //多個線程同時對集合進行修改
new Thread(() -> { //向集合中添加內容
list.add(UUID.randomUUID().toString().substring(0, 8)); //從集合中獲取內容
System.out.println(list); }, String.valueOf(i)).start(); } } }
3、原理分析
查看 Vector 的 add 方法
/** * Appends the specified element to the end of this Vector. * * @param e element to be appended to this Vector * @return {@code true} (as specified by {@link Collection#add}) * @since 1.2 */
public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; }
add 方法被 synchronized 同步修辭,線程安全!因此沒有並發異常。
三、方案二
1、使用 Collections 工具類
Collections 提供了方法 synchronizedList 保證 list 是同步線程安全的。

2、代碼實現
public class SafeListDemo { public static void main(String[] args) { List<String> list = Collections.synchronizedList(new ArrayList<>()); for (int i = 0; i < 30; i++) { //多個線程同時對集合進行修改
new Thread(() -> { //向集合中添加內容
list.add(UUID.randomUUID().toString().substring(0, 8)); //從集合中獲取內容
System.out.println(list); }, String.valueOf(i)).start(); } } }
3、原理分析
查看方法源碼:
/** * Returns a synchronized (thread-safe) list backed by the specified * list. In order to guarantee serial access, it is critical that * <strong>all</strong> access to the backing list is accomplished * through the returned list.<p> * * It is imperative that the user manually synchronize on the returned * list when iterating over it: * <pre> * List list = Collections.synchronizedList(new ArrayList()); * ... * synchronized (list) { * Iterator i = list.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> * Failure to follow this advice may result in non-deterministic behavior. * * <p>The returned list will be serializable if the specified list is * serializable. * * @param <T> the class of the objects in the list * @param list the list to be "wrapped" in a synchronized list. * @return a synchronized view of the specified list. */
public static <T> List<T> synchronizedList(List<T> list) { return (list instanceof RandomAccess ?
new SynchronizedRandomAccessList<>(list) : new SynchronizedList<>(list)); }
可以看到是幫我們創建了一個同步的集合。
以 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 同步鎖的方式來實現同步的,效率不高。
四、方案三(重點)
1、使用 CopyOnWriteArrayList
首先我們對 CopyOnWriteArrayList 進行學習,其特點如下:
它相當於線程安全的 ArrayList。和 ArrayList 一樣,它是個可變數組;但是和ArrayList 不同的時,它具有以下特性:
(1)它最適合於具有以下特征的應用程序: List 大小通常保持很小,只讀操作遠多於可變操作,需要在遍歷期間防止線程間的沖突;
(2)它是線程安全的;
(3)因為通常需要復制整個基礎數組,所以可變操作(add()、 set() 和 remove() 等等)的開銷很大;
(4)迭代器支持 hasNext(), next()等不可變操作,但不支持可變 remove()等操作;
(5)使用迭代器進行遍歷的速度很快,並且不會與其他線程發生沖突。在構造迭代器時,迭代器依賴於不變的數組快照;
2、代碼實現
public class SafeListDemo { public static void main(String[] args) {
List<String> list = new CopyOnWriteArrayList<>(); for (int i = 0; i < 30; i++) { //多個線程同時對集合進行修改
new Thread(() -> { //向集合中添加內容
list.add(UUID.randomUUID().toString().substring(0, 8)); //從集合中獲取內容
System.out.println(list); }, String.valueOf(i)).start(); } } }
3、核心思想
核心思想:獨占鎖效率低:采用讀寫分離思想解決
寫線程獲取到鎖,其他寫線程阻塞。
復制思想:
當我們往一個容器添加元素的時候,不直接往當前容器添加,而是先將當前容器進行 Copy,復制出一個新的容器,然后新的容器里添加元素,添加完元素之后,再將原容器的引用指向新的容器。
這時候會拋出來一個新的問題,也就是數據不一致的問題。如果寫線程還沒來得及寫會內存,其他的線程就會讀到了臟數據。
這就是 CopyOnWriteArrayList 的思想和原理。
4、原因分析:動態數組與線程安全
下面從“動態數組” 和“線程安全” 兩個方面進一步對CopyOnWriteArrayList 的原理進行說明。
“動態數組” 機制:
(1)它內部有個“volatile 數組”(array)來保持數據。在“添加/修改/刪除” 數據時,都會新建一個數組,並將更新后的數據拷貝到新建的數組中,最后再將該
數組賦值給“volatile 數組”, 這就是它叫做 CopyOnWriteArrayList 的原因;
(2)由於它在“添加/修改/刪除” 數據時,都會新建數組,所以涉及到修改數據的操作, CopyOnWriteArrayList 效率很低;但是單單只是進行遍歷查找的話,效率比較高。
“線程安全” 機制:
(1)通過 volatile 和互斥鎖來實現的。
(2)通過“volatile 數組” 來保存數據的。一個線程讀取 volatile 數組時,總能看到其它線程對該 volatile 變量最后的寫入;就這樣,通過 volatile 提供了“讀
取到的數據總是最新的” 這個機制的保證。
(3)通過互斥鎖來保護數據。在“添加/修改/刪除” 數據時,會先“獲取互斥鎖” ,再修改完畢之后,先將數據更新到“volatile 數組” 中,然后再“釋放互斥
鎖” ,就達到了保護數據的目的。
5、“寫時復制”
(1)不加鎖性能提升,出錯誤;加鎖數據一致,性能下降;
(2)CopyOnWriteArrayList 定義:
A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

/** * 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(); } }