单线程与线程池的性能对比

亲自尝试了之后才发现,虽然同是一个线程在工作,但是使用线程池效率竟然可以提升这么多!

代码如下:

 1 package cn.sp.test;
 2 
 3 import java.util.LinkedList;
 4 import java.util.List;
 5 import java.util.Random;
 6 import java.util.concurrent.LinkedBlockingQueue;
 7 import java.util.concurrent.ThreadPoolExecutor;
 8 import java.util.concurrent.TimeUnit;
 9 
10 public class MainMethod {
11     public static void main(String[] args) {
12         int count = 200000;
13         useThreadPool(count);
14         //useThread(count);
15     }
16     /**
17      * 使用线程池77ms
18      * @param count
19      */
20     public static void useThreadPool(int count){
21                 //开始时间
22                 long startTime = System.currentTimeMillis();
23                 //使用不可变类来保存线程安全性
24                 final List<Integer> list = new LinkedList<Integer>();
25                 //虽然线程最大数量被限制为1,但是可以重复使用,减少了创建和销毁线程的开销
26                 int corePoolSize = 1;
27                 int maximumPoolSize = 1;
28                 
29                 ThreadPoolExecutor tp = 
30                         new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(count));
31                 final Random random = new Random();//使用final 理由同上
32                 for(int i = 0 ; i<count ; i++){
33                     tp.execute(new Runnable() {
34                         
35                         @Override
36                         public void run() {
37                             list.add(random.nextInt());
38                             
39                         }
40                     });
41                 }
42                 
43                 //关闭线程池
44                 tp.shutdown();
45                 try {
46                     tp.awaitTermination(1, TimeUnit.SECONDS);
47                 } catch (InterruptedException e) {
48                     // TODO Auto-generated catch block
49                     e.printStackTrace();
50                 }
51                 //执行时间
52                 System.out.println(System.currentTimeMillis() - startTime);
53                 System.out.println(list.size());
54     }
55     /**
56      * 每次创建一个新的线程: 19760ms
57      * @param count
58      */
59     public static void useThread(int count){
60         //开始时间
61         long startTime = System.currentTimeMillis();
62         final List<Integer> list = new LinkedList<Integer>();
63         final Random random = new Random();
64         
65         for(int i=0 ; i< count ; i++){
66             Thread thread = new Thread(new Runnable() {
67                 
68                 @Override
69                 public void run() {
70                     // TODO Auto-generated method stub
71                     list.add(random.nextInt());
72                 }
73             });
74             
75             thread.start();
76             try {
77                 thread.join();//被调用后main线程将被一直阻塞,直到该线程执行完毕
78             } catch (InterruptedException e) {
79                 // TODO Auto-generated catch block
80                 e.printStackTrace();
81             }
82         }
83         
84         //执行时间
85         System.out.println(System.currentTimeMillis() - startTime);
86         System.out.println(list.size());
87     }
88 }

和我一样的多线程初学者可以试下。

 

转载于:https://www.cnblogs.com/2YSP/p/6930922.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值