范例1:
package com.contoso;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CopyOnWriteArrayListExample1 {
/**
* CopyOnWriteArrayList用来替换同步List对象
*
* 当读操作比写操作更频繁时,使用CopyOnWriteArrayList
*
* 每次修改列表时都创建一个新副本,只进行短暂地同步以确保数组内容的可见性
*
* 迭代器返回集合当前状态的快照
*
* 支持原子操作
*
*/
public static void useCopyOnWriteArrayList() {
System.out.println("=== CopyOnWriteArrayList ===");
ExecutorService executor = Executors.newCachedThreadPool();
Random random = new Random();
// 不会有ConcurrentModificationException异常抛出
CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList<Integer>();
for (int i = 0; i < 100; i++) {
if (i % 3 == 0) {
// 写操作
executor.execute(() -> {
int value = random.nextInt(50);
System.err.println("Write " + value);
copyOnWriteArrayList.add(value);
});
} else {
// 读操作
executor.execute(() -> {
StringBuffer buffer = new StringBuffer();
for (Object value : copyOnWriteArrayList) {
buffer.append(value + " ");
}
System.out.println("Read " + buffer.toString());
});
}
}
executor.shutdown();
}
public static void main(String[] args) {
useCopyOnWriteArrayList();
}
}
run:
=== CopyOnWriteArrayList ===
Write 13
Read 13
Read 13
Read 13
Read 13
Read 13
Read 13
Write 35
Read 13 35
Read 13 35
Write 5
Read 13 35 5
Write 1
Read 13 35 5 1
Read 13 35 5 1
Read 13 35 5 1
Write 17
Read 13 35 5 1 17
Read 13 35 5 1 17
Write 40
Read 13 35 5 1 17 40 46
Write 46
Write 49
Read 13 35 5 1 17 40 46
Write 35
Read 13 35 5 1 17 40 46 49 35 2
Write 2
Read 13 35 5 1 17 40 46 49 35 2
Write 34
Read 13 35 5 1 17 40 46 49 35 2 34
Read 13 35 5 1 17 40 46 49 35 2 34
Write 35
Read 13 35 5 1 17 40 46 49 35 2 34 35
Read 13 35 5 1 17 40 46 49 35 2 34 35
Read 13 35 5 1 17 40 46 49 35 2 34 35
Write 32
Read 13 35 5 1 17 40 46 49 35 2 34 35
Read 13 35 5 1 17 40 46 49 35 2 34 35 32
Read 13 35 5 1 17 40 46 49 35 2 34 35 32
Read 13 35 5 1 17 40 46 49 35 2 34 35 32
Read 13 35 5 1 17 40 46 49 35 2 34
Write 42
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42
Write 26
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26
Write 10
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26
Write 9
Write 39
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39
Write 35
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35
Write 41
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41
Write 5
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5
Write 9
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9
Write 25
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9
Write 36
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25
Write 0
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0
Write 3
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0
Write 18
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18
Write 38
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18
Write 9
Write 0
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0
Write 0
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0
Write 21
Write 36
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0 0
Write 48
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0 0 21 36
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0 0 21 36 48
Read 13 35 5 1 17 40 46 49 35 2 34 35 32 42 26 10 9 39 35 41 5 9 25 36 0 3 18 38 9 0 0 21 36
BUILD SUCCESSFUL (total time: 0 seconds)