共计 19712 个字符,预计需要花费 50 分钟才能阅读完成。
注:本系列文章中用到的 jdk 版本均为
java8
相比很多同学在刚接触 Java 汇合的时候,线程平安的 List 用的肯定是 Vector
。然而当初用到的线程平安的 List 个别都会用CopyOnWriteArrayList
,很少有人再去用Vector
了,至于为什么,文章中会具体说到。接下来,咱们先来简略剖析以下 Vector
的源码。
一、Vector 汇合源码简析
因为本文的重点不是 Vector
汇合,因而只是简略的剖析一下 Vector
的初始化办法和增加元素的办法。
Vector
的底层实现和 ArrayList
一样,都是由数组实现的。
Vector
的次要变量如下:
/**
* 寄存元素的数组
*/
protected Object[] elementData;
/**
* 元素个数
*/
protected int elementCount;
/**
* 扩容自增容量大小
*/
protected int capacityIncrement;
1.1 Vector 初始化
Vector
的初始化提供了三个办法,除了能够指定初始容量的大小,还能够指定扩容容量的大小。结构器别离如下:
无参结构器
public Vector() {this(10);
}
指定初始化容量的结构器
public Vector(int initialCapacity) {this(initialCapacity, 0);
}
指定初始化容量和扩容容量大小的结构器
public Vector(int initialCapacity, int capacityIncrement) {super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity:"+initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
从下面的结构器中能够看出,如果调用无参结构器,则会创立一个初始化容量为 10
,扩容容量为0
的Vector
汇合。
1.2 如何扩容
Vector
的扩容机制和 ArrayList
的很像,如果不分明 ArrayList
的扩容机制,能够看看这篇文章。这里咱们间接看 Vector
的扩容办法grow
。
private void grow(int minCapacity) {
// overflow-conscious code
// 初始化数组的长度,默认为 10
int oldCapacity = elementData.length;
// 是否指定扩容容量,不指定扩容为原来的 2 倍
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
通过下面的办法,咱们能够看出,如果指定了扩容容量的大小则扩容的新数组大小为原来的数组加上扩容容量的大小,如果不指定扩容容量的大小则扩容的新数组大小为原来数组大小的 2
倍。这样扩容为原来的 2 倍是很耗费空间的,这也是 Vector
被弃用的起因之一。
除此之外,看过源码的同学可能发现了,Vector
汇合的所有操作元素的办法都加了 synchronized
关键字,这就导致了操作 Vector
的效率会非常低,在开发中,往往读操作的应用频率会远高于其余操作,而 CopyOnWriteArrayList
就是这样一种读操作效率远高于写操作效率的 List,一起来看看。
二、CopyOnWriteArrayList 源码简析
CopyOnWriteArrayList
类图:
2.1 CopyOnWrite 思维
CopyOnWrite
简称 COW,依据名字来看就是 写入时复制。意思就是大家独特去拜访一个资源,如果有人想要去批改这个资源的时候,就须要复制一个正本,去批改这个正本,而对于其他人来说拜访得资源还是原来的,不会发生变化。
2.2 初始化 CopyOnWriteArrayList
CopyOnWriteArrayList
底层是也是有数组实现的。本文咱们只解读增加元素和读取元素的区别,删除批改元素原理和增加元素差不多,操作时都须要进行加锁,而读操作不会加锁。
CopyOnWriteArrayList
次要有以下两个变量:
// 独占锁
final transient ReentrantLock lock = new ReentrantLock();
// 寄存元素的数组
private transient volatile Object[] array;
咱们认真来剖析一下下面两个属性,这两个思维是 CopyOnWriteArrayList
的外围。
- lock:ReentrantLock,独占锁,多线程运行的状况下,只有一个线程会取得这个锁,只有开释锁后其余线程能力取得。
- array:存放数据的数组,要害是被
volatile
润饰了,被volatile
润饰,就保障了可见性,也就是一个线程批改后,其余线程立刻可见。
最罕用的初始化形式如下:
/**
* Creates an empty list.
*/
public CopyOnWriteArrayList() {setArray(new Object[0]);
}
/**
* Sets the array.
*/
final void setArray(Object[] a) {array = a;}
初始化只是创立了一个空的数组,并将 array
指向它。
2.3 增加元素
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
// 获取原来的数组
Object[] elements = getArray();
// 原来数组的长度
int len = elements.length;
// 创立一个长度 + 1 的新数组,并将原来数组的元素复制给新数组
Object[] newElements = Arrays.copyOf(elements, len + 1);
// 元素放在新数组开端
newElements[len] = e;
// array 指向新数组
setArray(newElements);
return true;
} finally {lock.unlock();
}
}
增加数组的步骤如下:
- 取得独占锁,将增加性能加锁
- 获取原来的数组,并失去其长度
- 创立一个长度为原来数组长度 + 1 的数组,并拷贝原来的元素给新数组
- 追加元素到新数组开端
- 指向新数组
- 开释锁
这个过程是线程平安的,COW 的核心思想就是每次批改的时候拷贝一个新的资源去批改,add()
办法再拷贝新资源的时候将数组容量 +1,这样尽管每次增加元素都会节约肯定的空间,然而数组的长度正好是元素的长度,也在肯定水平上节俭了扩容的开销。
2.4 获取元素 https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…
https://github.com/hitl837/ml…
https://www.github.com/hitl83…
http://github.com/hitl837/mlo…