Collections. SynchronizedList ()、CopyOnWriteArrayList 与 Vector 有什么区别?

Collections. SynchronizedList ()CopyOnWriteArrayListVector 都是线程安全的集合。

  1. Vector

    Vector 的同步做法是给所有公有方法都加上 “Synchronized”[^1] 关键字。

    1
    2
    3
    4
    5
    6
    7
    8
    //Vector
    public synchronized boolean add(E e) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = e;
    return true;
    }

阅读更多

Java集合

  1. Java 集合关系

  
整体上集合分为 CollectionMap, 所有集合的实现类都要实现其中一个。

阅读更多

checkForComodification

1
2
3
4
5
6
7
8
9
10
11
12
//执行这段代码是会抛出异常 ConcurrentModificationException
for (String str : list) {
if ("remove".equals(str)) {
list.remove(str);
}
}

//具体位置
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
阅读更多