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++) {
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(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("队列已满拒绝连接");
}
});