
從JDK1.5版本,JAVA提供了線程安全的List增強版CopyOnWriteArrayList,其保持線程安全的方式是:每次修改數據時,不會直接修改數據,而是把數據復制出來一份,對復制出來的數組進行操作。
通過這樣的機制,可以極大程度的提升讀的并發性能,所以對于CopyOnWriteArrayList來說,非常適合讀多寫少或者無鎖的場景。
但是,如果我們為了炫技而不分場合濫用CopyOnWriteArrayList的話,可能會帶來適得其反的結果。
下面,我們通過一段測試代碼,比較一下CopyOnWriteArrayList和普通加鎖ArrayList的讀寫性能差距。
我們首先測試一下寫性能的差距:構建一個CopyOnWriteArrayList和synchronizedList,通過多線程并發寫入100000個元素。
List<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
//構建一個加鎖的List
List<Integer> synchronizedList = Collections.synchronizedList(new ArrayList<>());
StopWatch stopWatch = new StopWatch();
int loopCount = 100000;
stopWatch.start("測試寫性能:copyOnWriteArrayList");
//多線程寫入100000個數字
IntStream.rangeClosed(1, loopCount).parallel()
.forEach(x -> copyOnWriteArrayList.add(ThreadLocalRandom.current().nextInt(loopCount))
);
stopWatch.stop();
stopWatch.start("測試寫性能:synchronizedList");
//多線程寫入100000個數字
IntStream.rangeClosed(1, loopCount).parallel()
.forEach(x -> synchronizedList.add(ThreadLocalRandom.current().nextInt(loopCount))
);
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
Map<String, Integer> result = new HashMap<>();
result.put("copyOnWriteArrayList", copyOnWriteArrayList.size());
result.put("synchronizedList", synchronizedList.size());
System.out.println(JSON.toJSONString(result));
可以清楚的看到,在大量寫的情況下,CopyOnWriteArrayList的性能是遠遠不如普通的加鎖List的,性能差距可能在100倍以上。

而之所以CopyOnWriteArrayList的寫入這么慢,就是因為CopyOnWriteArrayList每次寫入都要對存放元素的舊數組進行復制創建一個新數組,從而導致內存申請釋放消耗很大。

我們再測試一下大量讀的性能差距:先對兩個List寫入100000個元素,再通過多線程的方式隨機get元素。
List<Integer> synchronizedList = Collections.synchronizedList(new ArrayList<>());
synchronizedList.addAll(IntStream.rangeClosed(1, 100000).boxed().collect(Collectors.toList()));
List<Integer> copyOnWriteArrayList = IntStream.rangeClosed(1, 100000).boxed()
.collect(Collectors.toCollection(CopyOnWriteArrayList::new));
StopWatch stopWatch = new StopWatch();
int loopCount = 1000000;
int count = copyOnWriteArrayList.size();
stopWatch.start("測試讀性能:copyOnWriteArrayList");
IntStream.rangeClosed(1, loopCount).parallel().forEach(
x -> copyOnWriteArrayList.get(ThreadLocalRandom.current().nextInt(count))
);
stopWatch.stop();
stopWatch.start("測試讀性能:synchronizedList");
IntStream.range(0, loopCount).parallel().forEach(
x -> synchronizedList.get(ThreadLocalRandom.current().nextInt(count))
);
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
Map<String,Integer> result = new HashMap<>();
result.put("copyOnWriteArrayList", copyOnWriteArrayList.size());
result.put("synchronizedList", synchronizedList.size());
System.out.println(JSON.toJSONString(result));
經過多次測試,CopyOnWriteArrayList的讀性能大概在普通加鎖List的2-5倍左右。

而CopyOnWriteArrayList的讀之所以快,是因為CopyOnWriteArrayList讀取元素是無鎖狀態下直接按數組下標獲取。

一般來說,CopyOnWriteArrayList只適用于大量讀的場景,如果有了大量寫操作,性能反而不如普通的List。
JDK為我們提供了很多用于并發場景的工具類,但是仍需要我們仔細了解每一種工具的使用場景,在不合適的場景使用不合適的工具,會導致性能更差。