记录一次 org.springframework.core.task.TaskRejectedException

我的个人网站:等不见天亮等时光
  • 错误信息

Exception in thread "main" org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@1b701da1[Running, pool size = 2, active threads = 2, queued tasks = 15, completed tasks = 0]] did not accept task: com.sunyw.xyz.TestClassForTherd$TestThreadHander@58d25a40
	at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:296)
	at com.sunyw.xyz.TestClassForTherd.main(TestClassForTherd.java:31)
Caused by: java.util.concurrent.RejectedExecutionException: Task com.sunyw.xyz.TestClassForTherd$TestThreadHander@58d25a40 rejected from java.util.concurrent.ThreadPoolExecutor@1b701da1[Running, pool size = 2, active threads = 2, queued tasks = 15, completed tasks = 0]
	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
	at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)

  • 代码
package com.sunyw.xyz;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.locks.ReentrantLock;

public class TestClassForTherd {

    private static int Fords = 100000;

    private static final long time = System.currentTimeMillis();

    private static int i = 0;

    public static void main(String[] args) throws Exception {
        System.out.println("开始时间:" + time);
        /**
         * 线程池的五种创建方式
         */
        /**
         * 单例线程池,只有一个线程
         * */

        ThreadPoolTaskExecutor init = init();
        for (int id = 0; id <= Fords ; id++) {
            // ExecutorService signService = Executors.newSingleThreadExecutor();
            TestThreadHander testThreadHander = new TestThreadHander();
            testThreadHander.setName(i);
            init.execute(testThreadHander);
        }
        /* *//**
         * 缓存线程池,有多少个请求,就会创建多少个线程池,最大线程数量为int的最大值
         *//*
        ExecutorService cashService = Executors.newCachedThreadPool();

        *//**
         * 可以初始化线程池的数量,可以指定线程池使用的执行器
         *//*
        ThreadPoolTaskExecutor factory = new ThreadPoolTaskExecutor();
        factory.setMaxPoolSize(10);
        ExecutorService fixService = Executors.newFixedThreadPool(10, factory);
        *//**
         * 定时执行线程池,可以定时的去执行任务,定义queue的大小
         *//*
        ExecutorService sechService = Executors.newScheduledThreadPool(10);*/
    }

    private static ThreadPoolTaskExecutor init() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        //executor.setQueueCapacity(Integer.MAX_VALUE);
        executor.setQueueCapacity(15);
        executor.setThreadNamePrefix("线程测试----");
        executor.setKeepAliveSeconds(5);
        executor.setAllowCoreThreadTimeOut(true);
        executor.initialize();
        return executor;
    }

    /**
     * 线程执行处理类
     */
    static class TestThreadHander implements Runnable {
        private int name;

        void setName(int name) {
            this.name = name;
        }

        @Override
        public void run() {
            TestThread testThread = new TestThread();
            testThread.sout(name);
        }
    }

    public final ReentrantLock lock = new ReentrantLock();

    /**
     * 假设为业务方法
     */
    static class TestThread {
        synchronized void sout(int name) {
            System.out.println(i++);
            sou();
        }
    }

    private static void sou() {
        if (i == 100000) {
            long end = System.currentTimeMillis();
            System.out.println("结束时间:" + end);
            System.out.println("耗时:" + (time - end) + "ms");
        }
    }
}

  • for循环时,执行次数为10W次,原因是因为最大的消息计数器,已经超过了当前队列初始化的容量,导致触发器失效而被拒绝;
  • 处理方案:可以简洁暴力的在初始化队列时设置队列容量为最大值
executor.setQueueCapacity(Integer.MAX_VALUE);
  • 消息拒绝抛出,给出拒绝提示
 executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println("队列已满拒绝连接");
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值