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

96
程序汪丶
0.8 2019.08.06 21:22 字数 396

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

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

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

public class ConcurrencyTest {
    private static final long count = 10000l;

    public static void main(String[] args) throws InterruptedException {
        for(int i=0; i<10; i++) {
            concurrency();
            serial();
            System.out.println("------------------------------------------");
        }

    }

    private static void concurrency() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = 0;
                for (long i = 0; i < count; i++) {
                    a += 5;
                }
            }
        });
        thread.start();
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        thread.join();
        System.out.println("concurrency :" + time + "ms,b=" + b);
    }

    private static void serial() {
        long start = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < count; i++) {
            a += 5;
        }
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("serial:" + time + "ms,b=" + b + ",a=" + a);
    }
}

测试结果
这是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.因为数量较小的时候,并发会花费很多时间在线程调度上。

最后

在此我向大家推荐一个架构学习交流圈。点击加入交流圈 里面资深架构师会分享一些整理好的录制视频录像和BATJ面试题:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多。

文章出处:https://www.cnblogs.com/yangmingsen/p/11290707.html
作者: 杨铭森

JAVA