先写一段代码证明CopyOnWriteArrayList
确实是线程安全的。
ReadThread.java
<span style="color:#000000"><code><span style="color:#000088">import</span> java.util.List;
<span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">ReadThread</span> <span style="color:#000088">implements</span> <span style="color:#4f4f4f">Runnable</span> {
<span style="color:#000088">private</span> List<Integer> list;
<span style="color:#000088">public</span> <span style="color:#009900">ReadThread</span>(List<Integer> list) {
<span style="color:#000088">this</span>.list = list;
}
<span style="color:#9b859d">@Override</span>
<span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">run</span>() {
<span style="color:#000088">for</span> (Integer ele : list) {
System.out.println(<span style="color:#009900">"ReadThread:"</span>+ele);
}
}
}</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
WriteThread.java
<span style="color:#000000"><code><span style="color:#000088">import</span> java.util.List;
<span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">WriteThread</span> <span style="color:#000088">implements</span> <span style="color:#4f4f4f">Runnable</span> {
<span style="color:#000088">private</span> List<Integer> list;
<span style="color:#000088">public</span> <span style="color:#009900">WriteThread</span>(List<Integer> list) {
<span style="color:#000088">this</span>.list = list;
}
<span style="color:#9b859d">@Override</span>
<span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">run</span>() {
<span style="color:#000088">this</span>.list.add(<span style="color:#006666">9</span>);
}
}</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
TestCopyOnWriteArrayList.java
<span style="color:#000000"><code><span style="color:#000088">import</span> java.util.Arrays;
<span style="color:#000088">import</span> java.util.List;
<span style="color:#000088">import</span> java.util.concurrent.CopyOnWriteArrayList;
<span style="color:#000088">import</span> java.util.concurrent.ExecutorService;
<span style="color:#000088">import</span> java.util.concurrent.Executors;
<span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">TestCopyOnWriteArrayList</span> {
<span style="color:#000088">private</span> <span style="color:#000088">void</span> <span style="color:#009900">test</span>() {
<span style="color:#880000">//1、初始化CopyOnWriteArrayList</span>
List<Integer> tempList = Arrays.asList(<span style="color:#000088">new</span> Integer [] {<span style="color:#006666">1</span>,<span style="color:#006666">2</span>});
CopyOnWriteArrayList<Integer> copyList = <span style="color:#000088">new</span> CopyOnWriteArrayList<>(tempList);
<span style="color:#880000">//2、模拟多线程对list进行读和写</span>
ExecutorService executorService = Executors.newFixedThreadPool(<span style="color:#006666">10</span>);
executorService.execute(<span style="color:#000088">new</span> ReadThread(copyList));
executorService.execute(<span style="color:#000088">new</span> WriteThread(copyList));
executorService.execute(<span style="color:#000088">new</span> WriteThread(copyList));
executorService.execute(<span style="color:#000088">new</span> WriteThread(copyList));
executorService.execute(<span style="color:#000088">new</span> ReadThread(copyList));
executorService.execute(<span style="color:#000088">new</span> WriteThread(copyList));
executorService.execute(<span style="color:#000088">new</span> ReadThread(copyList));
executorService.execute(<span style="color:#000088">new</span> WriteThread(copyList));
System.out.println(<span style="color:#009900">"copyList size:"</span>+copyList.size());
}
<span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> <span style="color:#009900">main</span>(String[] args) {
<span style="color:#000088">new</span> TestCopyOnWriteArrayList().test();
}
}</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
运行上面的代码,没有报出
<span style="color:#000000"><code>java<span style="color:#009900">.util</span><span style="color:#009900">.ConcurrentModificationException</span></code></span>
- 1
说明了CopyOnWriteArrayList并发多线程的环境下,仍然能很好的工作。
CopyOnWriteArrayList如何做到线程安全的
CopyOnWriteArrayList
使用了一种叫写时复制的方法,当有新元素添加到CopyOnWriteArrayList
时,先从原有的数组中拷贝一份出来,然后在新的数组做写操作,写完之后,再将原来的数组引用指向到新数组。
当有新元素加入的时候,如下图,创建新数组,并往新数组中加入一个新元素,这个时候,array这个引用仍然是指向原数组的。
当元素在新数组添加成功后,将array这个引用指向新数组。
CopyOnWriteArrayList
的整个add操作都是在锁的保护下进行的。
这样做是为了避免在多线程并发add的时候,复制出多个副本出来,把数据搞乱了,导致最终的数组数据不是我们期望的。
CopyOnWriteArrayList
的add
操作的源代码如下:
<span style="color:#000000"><code> <span style="color:#000088">public</span> boolean <span style="color:#009900">add</span>(E e) {
<span style="color:#880000">//1、先加锁</span>
final ReentrantLock <span style="color:#000088">lock</span> = <span style="color:#000088">this</span>.<span style="color:#000088">lock</span>;
<span style="color:#000088">lock</span>.<span style="color:#000088">lock</span>();
<span style="color:#000088">try</span> {
Object[] elements = getArray();
<span style="color:#000088">int</span> len = elements.length;
<span style="color:#880000">//2、拷贝数组</span>
Object[] newElements = Arrays.copyOf(elements, len + <span style="color:#006666">1</span>);
<span style="color:#880000">//3、将元素加入到新数组中</span>
newElements[len] = e;
<span style="color:#880000">//4、将array引用指向到新数组</span>
setArray(newElements);
<span style="color:#000088">return</span> <span style="color:#000088">true</span>;
} <span style="color:#000088">finally</span> {
<span style="color:#880000">//5、解锁</span>
<span style="color:#000088">lock</span>.unlock();
}
}</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
由于所有的写操作都是在新数组进行的,这个时候如果有线程并发的写,则通过锁来控制,如果有线程并发的读,则分几种情况:
1、如果写操作未完成,那么直接读取原数组的数据;
2、如果写操作完成,但是引用还未指向新数组,那么也是读取原数组数据;
3、如果写操作完成,并且引用已经指向了新的数组,那么直接从新数组中读取数据。
可见,CopyOnWriteArrayList
的读操作是可以不用加锁的。
CopyOnWriteArrayList的使用场景
通过上面的分析,CopyOnWriteArrayList
有几个缺点:
1、由于写操作的时候,需要拷贝数组,会消耗内存,如果原数组的内容比较多的情况下,可能导致young gc
或者full gc
2、不能用于实时读的场景,像拷贝数组、新增元素都需要时间,所以调用一个set
操作后,读取到数据可能还是旧的,虽然CopyOnWriteArrayList
能做到最终一致性,但是还是没法满足实时性要求;
CopyOnWriteArrayList
合适读多写少的场景,不过这类慎用
因为谁也没法保证CopyOnWriteArrayList
到底要放置多少数据,万一数据稍微有点多,每次add/set都要重新复制数组,这个代价实在太高昂了。在高性能的互联网应用中,这种操作分分钟引起故障。
CopyOnWriteArrayList透露的思想
如上面的分析CopyOnWriteArrayList表达的一些思想:
1、读写分离,读和写分开
2、最终一致性
3、使用另外开辟空间的思路,来解决并发冲突