Collections.synchronizedList与CopyOnWriteArrayList比较

1.单线程方式

2.多线程版本,不安全的 ArrayList

3.多线程版本,线程安全,CopyOnWriteArrayList()方式

4.多线程版本,线程安全,Collections.synchronizedList方式

  1 import java.util.ArrayList;
  2 import java.util.Collections;
  3 import java.util.List;
  4 import java.util.concurrent.CopyOnWriteArrayList;
  5 import java.util.concurrent.ExecutorService;
  6 import java.util.concurrent.Executors;
  7 
  8 /**
  9  * @author zsh
 10  * @site qqzsh.top
 11  * @create 2019-08-26 11:53
 12  * @description Collections.synchronizedList与CopyOnWriteArrayList比较
 13  * https://liuyanzhao.com/9732.html
 14  */
 15 public class Main3 {
 16 
 17     /**
 18      * 单线程:性能较差
 19      */
 20     static void f1(){
 21         Long startTime = System.currentTimeMillis();
 22         //这是一个长度为1000的集合
 23         List<Long> sourceList = new ArrayList<>();
 24         for (long i = 0L; i < 1000L; i++) {
 25             sourceList.add(i);
 26         }
 27         System.out.println("原列表大小:" + sourceList.size());
 28         //对原列表进行处理
 29         List<Long> resultList = new ArrayList<>();
 30         for (Long x : sourceList) {
 31             //模拟耗时操作(x累加300万次)
 32             Long sum = 0L;
 33             for (long i = 0L; i < 3000000L; i++) {
 34                 sum += x;
 35             }
 36             resultList.add(sum);
 37         }
 38         System.out.println("处理后的列表大小:" + resultList.size());
 39         System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
 40     }
 41 
 42     /**
 43      * 多线程版本,不安全的 ArrayList
 44      */
 45     static void f2(){
 46         Long startTime = System.currentTimeMillis();
 47         ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
 48         //这是一个长度为1000的集合
 49         List<Long> sourceList = new ArrayList<>();
 50         for (long i = 0L; i < 1000L; i++) {
 51             sourceList.add(i);
 52         }
 53         System.out.println("原列表大小:" + sourceList.size());
 54         List<Long> resultList = new ArrayList<>();
 55         for (Long x : sourceList) {
 56             fixedThreadPool.execute(new Runnable() {
 57                 @Override
 58                 public void run() {
 59                     //对原列表进行处理
 60                     //模拟耗时操作(x累加300万次)
 61                     Long sum = 0L;
 62                     for (long i = 0L; i < 3000000L; i++) {
 63                         sum += x;
 64                     }
 65                     resultList.add(sum);
 66                 }
 67             });
 68         }
 69         fixedThreadPool.shutdown();//关闭线程池
 70         //此处不可以删除或注释,需要线程执行结束后再执行别的内容,即只有线程结束后才会继续向下执行
 71         while (!fixedThreadPool.isTerminated()) {
 72         }
 73         System.out.println("处理后的列表大小:" + resultList.size());
 74         System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
 75     }
 76 
 77     /**
 78      * 多线程版本,线程安全,CopyOnWriteArrayList()方式
 79      */
 80     static void f3(){
 81         Long startTime = System.currentTimeMillis();
 82         ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
 83         //这是一个长度为1000的集合
 84         List<Long> sourceList = new ArrayList<>();
 85         for (long i = 0L; i < 1000L; i++) {
 86             sourceList.add(i);
 87         }
 88         System.out.println("原列表大小:" + sourceList.size());
 89         List<Long> resultList = new CopyOnWriteArrayList<>();
 90         for (Long x : sourceList) {
 91             fixedThreadPool.execute(new Runnable() {
 92                 @Override
 93                 public void run() {
 94                     //对原列表进行处理
 95                     //模拟耗时操作(x累加300万次)
 96                     Long sum = 0L;
 97                     for (long i = 0L; i < 3000000L; i++) {
 98                         sum += x;
 99                     }
100                     resultList.add(sum);
101                 }
102             });
103         }
104         fixedThreadPool.shutdown();//关闭线程池
105         //此处不可以删除或注释,需要线程执行结束后再执行别的内容,即只有线程结束后才会继续向下执行
106         while (!fixedThreadPool.isTerminated()) {
107         }
108         System.out.println("处理后的列表大小:" + resultList.size());
109         System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
110     }
111 
112     /**
113      * 多线程版本,线程安全,Collections.synchronizedList方式
114      */
115     static void f4(){
116         Long startTime = System.currentTimeMillis();
117         ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
118         //这是一个长度为1000的集合
119         List<Long> sourceList = new ArrayList<>();
120         for (long i = 0L; i < 1000L; i++) {
121             sourceList.add(i);
122         }
123         System.out.println("原列表大小:" + sourceList.size());
124         List<Long> resultList = Collections.synchronizedList(new ArrayList<>());
125         for (Long x : sourceList) {
126             fixedThreadPool.execute(new Runnable() {
127                 @Override
128                 public void run() {
129                     //对原列表进行处理
130                     //模拟耗时操作(x累加300万次)
131                     Long sum = 0L;
132                     for (long i = 0L; i < 3000000L; i++) {
133                         sum += x;
134                     }
135                     resultList.add(sum);
136                 }
137             });
138         }
139         fixedThreadPool.shutdown();//关闭线程池
140         //此处不可以删除或注释,需要线程执行结束后再执行别的内容,即只有线程结束后才会继续向下执行
141         while (!fixedThreadPool.isTerminated()) {
142         }
143         System.out.println("处理后的列表大小:" + resultList.size());
144         System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
145     }
146 
147     public static void main(String[] args) {
148         /*f1();*/
149         /*f2();*/
150         /*f3();*/
151         f4();
152     }
153 }
View Code

 

posted @ 2019-08-26 14:40  张顺海  阅读(773)  评论(0编辑  收藏  举报