CopyOnWriteArrayList是ArrayList 的一個線程安全的變體,其中全部可變操做(add、set等等)都是經過對底層數組進行一次新的複製來實現的。 java
這通常須要很大的開銷,可是當遍歷操做的數量大大超過可變操做的數量時,這種方法可能比其餘替代方法更 有效。在不能或不想進行同步遍歷,但又須要從併發線程中排除衝突時,它也頗有用。「快照」風格的迭代器方法在建立迭代器時使用了對數組狀態的引用。此數組 在迭代器的生存期內不會更改,所以不可能發生衝突,而且迭代器保證不會拋出ConcurrentModificationException。建立迭代器 之後,迭代器就不會反映列表的添加、移除或者更改。在迭代器上進行的元素更改操做(remove、set和add)不受支持。這些方法將拋出 UnsupportedOperationException。容許使用全部元素,包括null。 數組
內存一致性效果:當存在其餘併發 collection 時,將對象放入CopyOnWriteArrayList以前的線程中的操做 happen-before 隨後經過另外一線程從CopyOnWriteArrayList中訪問或移除該元素的操做。 緩存
這種狀況通常在多線程操做時,一個線程對list進行修改。一個線程對list進行fore時會出現java.util.ConcurrentModificationException錯誤。 安全
下面來看一個列子:兩個線程一個線程fore一個線程修改list的值。
多線程
?併發
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package
com.lucky.concurrent.list;
import
java.util.ArrayList;
import
java.util.List;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
public
class
CopyOnWriteArrayListDemo {
/**
* 讀線程
* @author wangjie
*
*/
private
static
class
ReadTask
implements
Runnable {
List<String> list;
public
ReadTask(List<String> list) {
this
.list = list;
}
public
void
run() {
for
(String str : list) {
System.out.println(str);
}
}
}
/**
* 寫線程
* @author wangjie
*
*/
private
static
class
WriteTask
implements
Runnable {
List<String> list;
int
index;
public
WriteTask(List<String> list,
int
index) {
this
.list = list;
this
.index = index;
}
public
void
run() {
list.remove(index);
list.add(index,
"write_"
+ index);
}
}
public
void
run() {
final
int
NUM =
10
;
List<String> list =
new
ArrayList<String>();
for
(
int
i =
0
; i < NUM; i++) {
list.add(
"main_"
+ i);
}
ExecutorService executorService = Executors.newFixedThreadPool(NUM);
for
(
int
i =
0
; i < NUM; i++) {
executorService.execute(
new
ReadTask(list));
executorService.execute(
new
WriteTask(list, i));
}
executorService.shutdown();
}
public
static
void
main(String[] args) {
new
CopyOnWriteArrayListDemo().run();
}
}
|
從結果中能夠看出來。在多線程狀況 下報錯。其緣由就是多線程操做結果:那這個種方案不行咱們就換個方案。用jdk自帶的類CopyOnWriteArrayList來作容器。這個類和 ArrayList最大的區別就是add(E) 的時候。容器會自動copy一份出來而後再尾部add(E)。看源碼:
性能
?this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
*/
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();
}
}
|
用到了Arrays.copyOf 方法。這樣致使每次操做的都不是同一個引用。也就不會出現java.util.ConcurrentModificationException錯誤。
換了種方案看代碼:
spa
1
2
|
// List<String> list = new ArrayList<String>();
CopyOnWriteArrayList<String> list =
new
CopyOnWriteArrayList<String>();
|
也就把容器list換成了 CopyOnWriteArrayList,其餘的沒變。線程裏面的list不用改。由於 CopyOnWriteArrayList實現的也是list<E> 接口。看結果:
其結果沒報錯。
CopyOnWriteArrayList add(E) 和remove(int index)都是對新的數組進行修改和新增。因此在多線程操做時不會出現java.util.ConcurrentModificationException錯誤。
因此最後得出結論:CopyOnWriteArrayList適合使用在讀操做遠遠大於寫操做的場景裏,好比緩存。發生修改時候作copy,新老版本分離,保證讀的高性能,適用於以讀爲主的狀況。