ArrayList is thread unsafe, so in concurrent programming, collections.synchronized list and CopyOnWriteArrayList are often used to replace ArrayList. Next, we compare the performance of the two lists. Collection.synchronizedlis uses a synchronization lock in the update operation, while CopyOnWriteArrayList not only uses a reentrant lock in the update operation, but also needs to copy the array. The following is a comparison of the insert and read operations of the two synchronization classes to compare their speed.
import java.util.*; import java.util.Collections; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; /** * * @author tanhuiling *Collections.synchronizedList And CopyOnWriteArrayList performance analysis */ public class ListTest { private static List<String> arrayList = Collections.synchronizedList(new ArrayList<String>()); private static List<String> copyOnWriteArrayList = new CopyOnWriteArrayList<String>(); private static CountDownLatch cdl1 = new CountDownLatch(2); private static CountDownLatch cdl2 = new CountDownLatch(2); private static CountDownLatch cdl3 = new CountDownLatch(2); private static CountDownLatch cdl4 = new CountDownLatch(2); //ArrayList write thread static class ArrayAddThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<50000;i++) { arrayList.add(String.valueOf(i)); } cdl1.countDown(); } } //ArrayList read thread static class ArrayGetThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub int size = arrayList.size(); for(int i=0;i<size;i++) { arrayList.get(i); } cdl2.countDown(); } } //CopyOnWriteArrayList write thread static class CopyAddThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<50000;i++) { copyOnWriteArrayList.add(String.valueOf(i)); } cdl3.countDown(); } } //CopyOnWriteArrayList write thread static class CopyGetThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub int size = copyOnWriteArrayList.size(); for(int i=0;i<size;i++) { copyOnWriteArrayList.get(i); } cdl4.countDown(); } } public static void main(String[] args) throws InterruptedException { long start1 = System.currentTimeMillis(); new ArrayAddThread().start(); new ArrayAddThread().start(); cdl1.await(); long end1 = System.currentTimeMillis(); System.out.println("ArrayList Write operation time:"+(end1-start1)); long start3 = System.currentTimeMillis(); new CopyAddThread().start(); new CopyAddThread().start(); cdl3.await(); long end3 = System.currentTimeMillis(); System.out.println("CopyOnWriteArrayList Write time:"+(end3-start3)); long start2 = System.currentTimeMillis(); new ArrayGetThread().start(); new ArrayGetThread().start(); cdl2.await(); long end2 = System.currentTimeMillis(); System.out.println("ArrayList Read operation time:"+(end2-start2)); long start4 = System.currentTimeMillis(); new CopyGetThread().start(); new CopyGetThread().start(); cdl4.await(); long end4 = System.currentTimeMillis(); System.out.println("CopyOnWriteArrayList Read operation time of:"+(end4-start4)); } }
The test results are as follows:
ArrayList write operation time: 30 CopyOnWriteArrayList write time: 5710 ArrayList read operation time: 28 CopyOnWriteArrayList read time: 2
From the above results, we can see that the synchronized ArrayList write operation of collections.synchronized list uses synchronous lock, which is obviously faster than CopyOnWriteArrayListsh using lock plus array copy. At the same time, CopyOnWriteArrayList has faster read operations and better concurrency.