自定义线程池,自定义拒绝策略,不放弃任何一个任务(数据量小)

该博客展示了如何创建一个自定义的线程池,包括核心线程数、最大线程数、线程存活时间、工作队列和拒绝策略的配置。当队列满且线程达到最大数量时,采用阻塞策略将任务重新放入队列。同时,自定义了线程工厂和拒绝执行处理器来记录异常和处理报警。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class CustomThreadPoolExcutor {

    private ThreadPoolExecutor pool = null;

    public ThreadPoolExecutor getCustomThreadPoolExcutor(){
        return this
                .pool;
    }

    public void init(){
        pool = new ThreadPoolExecutor(1, 3 , 30 , TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(3), new CustomThreadFactory(),
                new CustomRejectedExecutionHandler());
    }

    public void destory(){
        if(pool != null){
            pool.shutdownNow();
        }
    }


    private class CustomThreadFactory implements ThreadFactory{
       private AtomicInteger count =  new AtomicInteger(0);


        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            String threadName = CustomThreadPoolExcutor.class.getSimpleName()+ count.addAndGet(1);
            System.out.println(threadName);
            thread.setName(threadName);
            return thread;
        }
    }

//阻塞
    private class CustomRejectedExecutionHandler implements RejectedExecutionHandler{


        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//            记录异常,报警处理等
//            System.out.println("error.....");
            System.out.println("队列满了,而且线程数量达到了最大数量啦");
            try {
                executor.getQueue().put(r);
                System.out.println("卡住了吗??");
            } catch (InterruptedException e) {
                System.out.println("塞入队列异常了");
            }
            System.out.println("入队ok");
        }

    }

    public static void main(String[] args) {
        CustomThreadPoolExcutor customThreadPoolExcutor = new CustomThreadPoolExcutor();
        customThreadPoolExcutor.init();
        ThreadPoolExecutor pool = customThreadPoolExcutor.getCustomThreadPoolExcutor();

        for (int i = 0; i < 10; i++) {

            System.out.println("提交第"+i+"个任务");
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    work();
                }
            });
            System.out.println("当前队列的剩余容量:"+pool.getQueue().remainingCapacity());
        }

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void work() {
        try{
            System.out.println("线程: " + Thread.currentThread().getName() + "进来啦!");
            Thread.sleep(1000L);
        } catch (Exception e) {

        }
    }

}

注意点:

executor.getQueue().put(r);
pool.getQueue().remainingCapacity();//查看当前队列长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值