zl程序教程

您现在的位置是:首页 >  后端

当前栏目

5.串行执行和并发执行的效率对比

效率并发执行 对比 串行
2023-06-13 09:14:00 时间

并发执行利用了线程的手段,可以提高执行效率,这里用一个简单的例子来进行简单对比

1.串行执行

public class ThreadDemo2 {
    private static long count = 100_0000_0000L;

    public static void main(String[] args) {
       // serial();
        concurrency();
    }

    //1.如果采用串行执行  大概是10s
    private static void serial() {
        long bed = System.currentTimeMillis();//时间戳//1970年1月1日0时0分0s为基准时刻,计算按当时的时刻和基准时刻之间的秒数只差

        int a = 0;
        for (long i = 0; i < count; i++){
            a++;
        }
        int b = 0;
        for (long i = 0; i < count ;i++){
            b++;
        }

        long end = System.currentTimeMillis();
        System.out.println("time:" + (end-bed) + "ms");
    }
}

串行执行的方法效率较低,此程序运行结果为: 可以看待时间为:8369ms

2.并发执行(线程)

public class ThreadDemo2 {
    private static long count = 100_0000_0000L;

    public static void main(String[] args) {
        serial();
        concurrency();
    }
    
    //2.创建线程来并发执行完成  3858ms
    private static void concurrency() {
        long beg = System.currentTimeMillis();

        //匿名内部类
        Thread t1 = new Thread(){
            @Override
            public void run() {
                int a = 0;
                for (long i = 0; i < count; i++){
                    a++;
                }
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {
                int b = 0;
                for (long i = 0; i < count; i++){
                    b++;
                }
            }
        };
        t1.start();
        t2.start();
        try {
            //线程等待,让主线程等待t1和t2执行结束,然后再继续往下执行
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("time:"+ (end-beg) + "ms");
    }
}

利用线程来并发执行的方法效率较高,此程序运行结果为: 可以看待时间为:3858ms