首页 > 编程语言 > 详细

多线程并发一定比单线程快吗?

时间:2019-08-02 19:23:47      阅读:84      评论:0      收藏:0      [点我收藏+]

 很多时候我们都以为要想处理速度更快,那就多开几个线程跑!

 确实多线程在一定情况下比单线程更快。

 下面的代码演示串行和并发执行并累加操作的时间,请分析:下面的代码并发执行一定比串行执行快吗?当count的数量增加 1万 -> 10万 -> 100万 -> 1000万 -> 1亿

 1 public class ConcurrencyTest {
 2     private static final long count = 10000l;
 3 
 4     public static void main(String[] args) throws InterruptedException {
 5         for(int i=0; i<10; i++) {
 6             concurrency();
 7             serial();
 8             System.out.println("------------------------------------------");
 9         }
10 
11     }
12 
13     private static void concurrency() throws InterruptedException {
14         long start = System.currentTimeMillis();
15         Thread thread = new Thread(new Runnable() {
16             @Override
17             public void run() {
18                 int a = 0;
19                 for (long i = 0; i < count; i++) {
20                     a += 5;
21                 }
22             }
23         });
24         thread.start();
25         int b = 0;
26         for (long i = 0; i < count; i++) {
27             b--;
28         }
29         long time = System.currentTimeMillis() - start;
30         thread.join();
31         System.out.println("concurrency :" + time + "ms,b=" + b);
32     }
33 
34     private static void serial() {
35         long start = System.currentTimeMillis();
36         int a = 0;
37         for (long i = 0; i < count; i++) {
38             a += 5;
39         }
40         int b = 0;
41         for (long i = 0; i < count; i++) {
42             b--;
43         }
44         long time = System.currentTimeMillis() - start;
45         System.out.println("serial:" + time + "ms,b=" + b + ",a=" + a);
46     }
47 }

测试结果

这是count=1万的结果,基本上串行要快些

并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:1ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:1ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:1ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------

 

这是count=10万l的结果,基本上串行要快点点

并发 :4ms,b=-100000
串行:3ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:1ms,b=-100000,a=500000
------------------------------------------
并发 :1ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:1ms,b=-100000,a=500000
------------------------------------------
并发 :1ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :1ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:1ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------

 

这是count=100万l的结果,基本上并发就要比串行要快了

并发 :5ms,b=-1000000
串行:5ms,b=-1000000,a=5000000
------------------------------------------
并发 :3ms,b=-1000000
串行:7ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:7ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :2ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:3ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:3ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------

余下的读者可以自己去调试哦。

 

结论

 1.在数量比较小的时候,很明显串行要比并发更快。

 2.因为数量较小的时候,并发会花费很多时间在线程调度上。

 

多线程并发一定比单线程快吗?

原文:https://www.cnblogs.com/yangmingsen/p/11290707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!