构造一个使用特定名称线程的线程池(ThreadPoolExecutor)

文章目录

代码

public class TestThreadPoolExecutor {

    public static void main(String[] args) {
        /**********************************参数begin**********************************/
        // 核心线程数
        int corePoolSize = 5;
        // 最大线程数
        int maximumPoolSize = 10;
        // 线程空闲时间(最大线程数-核心线程数多余的线程执行完了,多久后停掉)
        long keepAliveTime = 10;
        // 时间单位
        TimeUnit timeUnit = TimeUnit.SECONDS;
        // 任务阻塞队列
        BlockingQueue workQueue = new ArrayBlockingQueue<>(5);
        // 拒绝策略
        // 1、抛异常(默认)
        ThreadPoolExecutor.AbortPolicy abortPolicy = new ThreadPoolExecutor.AbortPolicy();
        // 2、用调用者所在的线程来执行任务
        ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
        // 3、丢弃阻塞队列中靠最前(最老)的任务,并执行当前任务
        ThreadPoolExecutor.DiscardOldestPolicy discardOldestPolicy = new ThreadPoolExecutor.DiscardOldestPolicy();
        // 4、直接丢弃任务
        ThreadPoolExecutor.DiscardPolicy discardPolicy = new ThreadPoolExecutor.DiscardPolicy();
        /**********************************参数end**********************************/

        ThreadPoolExecutor executorA = new ThreadPoolExecutor(
                corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, new NamedThreadFactory("AAAAA"),abortPolicy);
        ThreadPoolExecutor executorB = new ThreadPoolExecutor(
                corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, new NamedThreadFactory("BBBBB"),abortPolicy);

        executorA.execute(new Runnable() {
            @Override
            public void run() {
                int i = 1/0;
            }
        });
        executorB.execute(new Runnable() {
            @Override
            public void run() {
                System.out.printf("executorB处理业务");
            }
        });
        executorA.shutdown();
        executorB.shutdown();
    }


    /**
     * @Author lijie
     * @Description 自定义线程工厂
     * @Date 2022/4/20 21:47
     **/
    static class NamedThreadFactory implements ThreadFactory {
        private final AtomicInteger poolNumber = new AtomicInteger(1);
        private final ThreadGroup threadGroup;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        public final String namePrefix;

        NamedThreadFactory(String name) {
            SecurityManager s = System.getSecurityManager();
            threadGroup = (s != null) ? s.getThreadGroup() :
                    Thread.currentThread().getThreadGroup();
            if (null == name || "".equals(name.trim())) {
                name = "pool";
            }
            namePrefix = name + "-" +
                    poolNumber.getAndIncrement() +
                    "-thread-";
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(threadGroup, r,
                    namePrefix + threadNumber.getAndIncrement(),
                    0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李_杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值