rejected from java.util.concurrent.ThreadPoolExecutor@4ee285c6[Running, pool size = 5, active threads = 5, queued tasks = 3, completed tasks = 0]
这里看到显示被拒绝了,意思就是说原有定义的线程数不够用了,而我定义的又是拒绝策略,因为出现如上报错,

代码部分
package com.java.thread;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2025/4/30 20:15
*/
public class ThreadPoolTest {
private final static Logger logger= LoggerFactory.getLogger(ThreadPoolTest.class);
public static void main(String[] args) {
createThread();
}
public static void createThread() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
5,
60L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(3),
new QiuXieThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 10; i++) {
final int taskId = i;
executor.execute(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
throw new RuntimeException(e);
}
logger.info(taskId + "," + Thread.currentThread().getName());
});
}
}
}
很显然,这样是很不合理的,因为不能因为没有资源就赶人走你说是吧,当任务无人能接时,交给主线程做决定,简单来说就是排除大堂经理安排顾客


看到没,这里就不报错了
代码部分
package com.java.thread;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2025/4/30 20:15
*/
public class ThreadPoolTest {
private final static Logger logger= LoggerFactory.getLogger(ThreadPoolTest.class);
public static void main(String[] args) {
createThread();
}
public static void createThread() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
5,
60L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(3),
new QiuXieThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 10; i++) {
final int taskId = i;
executor.execute(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
throw new RuntimeException(e);
}
logger.info(taskId + "," + Thread.currentThread().getName());
});
}
}
}
但是也是有个问题的,如果顾客太多,大堂经理也是会崩溃的,那要咋办呢
咱可以动态调整服务人员嘛

代码部分
package com.java.thread;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2025/4/30 20:15
*/
public class ThreadPoolTest {
private final static Logger logger= LoggerFactory.getLogger(ThreadPoolTest.class);
public static void main(String[] args) {
createThread();
}
public static void createThread() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
5,
60L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(3),
new QiuXieThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 100; i++) {
final int taskId = i;
executor.execute(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
throw new RuntimeException(e);
}
logger.info(taskId + "," + Thread.currentThread().getName());
});
// 动态调整线程池大小
if (i > 5) {
executor.setCorePoolSize(4); // 增加核心线程数
executor.setMaximumPoolSize(8); // 增加最大线程数
logger.info("核心线程数:{},最大线程数:{}",executor.getCorePoolSize(),executor.getMaximumPoolSize());
}
}
}
}
除了写死,还可以动态获取队列中任务数量
代码部分
package com.java.thread;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2025/4/30 20:15
*/
public class ThreadPoolTest {
private final static Logger logger= LoggerFactory.getLogger(ThreadPoolTest.class);
public static void main(String[] args) {
createThread();
}
public static void createThread() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
5,
60L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(3),
new QiuXieThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 100; i++) {
final int taskId = i;
executor.execute(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
throw new RuntimeException(e);
}
logger.info(taskId + "," + Thread.currentThread().getName());
});
// 动态调整线程池大小
if (executor.getQueue().size() >= 3) {
executor.setCorePoolSize(4); // 增加核心线程数
executor.setMaximumPoolSize(8); // 增加最大线程数
logger.info("核心线程数:{},最大线程数:{}",executor.getCorePoolSize(),executor.getMaximumPoolSize());
}
}
}
}
pom依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.12</version>
<scope>runtime</scope>
</dependency>
浙公网安备 33010602011771号