关于java线程并发执行批量任务的效率优化探讨

package org.xxkk.utils.util;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author xxkk
 * @version 1.0.0
 * @title ThreadTest
 * @description ʕ•ᴥ•ʔ
 * @create 2024/7/27 22:55
 **/
public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {
        long begin = System.currentTimeMillis();
        ThreadTest test = new ThreadTest();
//        test.task1_test();
//        test.task2_test();
        test.task3_test();
        long end = System.currentTimeMillis();
        long spend_t = (end - begin) / 1000;
        System.out.println("总耗时:" + spend_t);
    }

    private void task1_test() {
        List<Runnable> task = new LinkedList<>();
        int tn = 10000000;
        for (int i = 0; i < tn; i++) {
            if (i % 100000 == 0)
                System.out.println("Remaining:" + (tn - i));
            task.add(new Runnable() {
                @Override
                public void run() {
                    JsonUtils.toJsonObj("{\"obj\":1}");
                }
            });
        }
        ExecutorService es = Executors.newFixedThreadPool(2);
        task.forEach(es::execute);
        es.shutdown();
        while (!es.isTerminated()) {
        }
    }
    private void task2_test() {
        ExecutorService es = Executors.newFixedThreadPool(2);
        int tn = 10000000;
        for (int i = 0; i < tn; i++) {
            if (i % 100000 == 0)
                System.out.println("Remaining:" + (tn - i));
            es.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        JsonUtils.toJsonObj("{\"obj\":1}");
                    } catch (Exception e) {
                    }
                }
            });
        }
        es.shutdown();
        while (es.isTerminated()){}

    }
    private void task3_test() throws InterruptedException {
        ExecutorService es = Executors.newFixedThreadPool(2);
        int tn = 10000000;
        CountDownLatch latch = new CountDownLatch(tn);
        for (int i = 0; i < tn; i++) {
            if (i % 100000 == 0)
                System.out.println("Remaining:" + (tn - i));
            es.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        JsonUtils.toJsonObj("{\"obj\":1}");
                    } catch (Exception e) {
                    }finally {
                        latch.countDown();
                    }
                }
            });
        }
        es.shutdown();
        latch.await();
    }
}

分别执行任务1、2、3耗时分别为12、15、19

任务1cpu图

任务2cpu图

本人不知为何for循环内创建并执行多线程和for循环内创建任务线程并在循环外一并提交会出现cpu图谱的如此差异?

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值