线程池命名

博客提及在项目中添加了hutool - all.jar工具类,给出了工具类文档链接https://hutool.cn/docs/#/ ,未提及执行结果相关具体信息技术内容。

如何优雅的用线程池创建线程并且命名


大家都知道阿里巴巴规范手册说要给线程命名,话不多说直接贴代码



import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.ThreadPoolExecutor;

public class RunFirst {
    private static ThreadPoolExecutor threadPool = null;

    static {
        threadPool = ExecutorBuilder.create()
                .setCorePoolSize(10)
                .setMaxPoolSize(20)
                .setKeepAliveTime(1L, TimeUnit.SECONDS)
                .useArrayBlockingQueue(100000)
                .setThreadFactory(new NamedThreadFactory("my-task-", false))
                .setAllowCoreThreadTimeOut(true)
                .build();
    }


    public static void main(String[] args) {
        System.out.println("开始执行");
        int len = 10000000;//1亿次
        for (int i = 0; i < len; i++) {
            int finalI = i;
            threadPool.execute(() -> {
                System.out.println("当前线程名=" + Thread.currentThread().getName() + "==>工作任务是打印:" + finalI);
            });
            System.out.println("```当前线程名=" + Thread.currentThread().getName() + "==>工作任务是打印:" + finalI);
        }
        System.out.println("执行完毕");
    }
}

最后执行结果是
执行结果

我只是在项目中添加了一个hutool-all.jar

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.4</version>
</dependency>

工具类 https://hutool.cn/docs/#/

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值