Spring Bean 的延迟初始化
Spring 使用 @EnableAspectJAutoProxy 注解启用 AOP
Spring:在 @PostConstruct、InitializingBean、ApplicationContextAware、ApplicationListener 等中抛出异常会发生什么?
Spring:在 @PostConstruct 中设置其他 Bean 中变量可能引发的问题
Spring @Order 注解
Spring:使用 @DepoendsOn 调整 Bean 初始化顺序
Spring 使用 BeanUtils.copyProperties 复制 Java Bean 属性
Spring AopUtils 工具类
Spring 结合 junit4 编写单元测试
Spring 结合 Mockito 编写单元测试
Spring ReflectionTestUtils 工具类
Spring 测试:使用 PowerMock mock 静态方法
Spring JdbcTemplate 快速入门
Spring @Transactional 的自调用问题
Spring 获取代理对象中被代理的目标对象
Spring:ThreadPoolTaskExecutor 线程池的使用
Spring:ThreadPoolTaskExecutor 线程池等待所有任务完成的几种方式
Spring:获取线程池 ThreadPoolTaskExecutor 任务队列的待执行任务数量
Spring 源码分析:分析 Hello World 程序
Spring 源码分析:ApplicationContext
Spring 源码分析:@Bean 原理
Spring 源码分析:@PostConstruct、InitializingBean、ApplicationContextAware、ApplicationListener 的执行顺序
Spring:获取线程池 ThreadPoolTaskExecutor 任务队列的待执行任务数量
#Spring
使用
threadPoolTaskExecutor.getThreadPoolExecutor().getQueue().size()
即可。
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class TestThreadPool {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setThreadNamePrefix("xxxx-");
threadPoolTaskExecutor.setCorePoolSize(1);
threadPoolTaskExecutor.setMaxPoolSize(5);
threadPoolTaskExecutor.setQueueCapacity(5);
threadPoolTaskExecutor.setKeepAliveSeconds(300);
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
System.out.println("丢弃");
threadPoolTaskExecutor.initialize();
int taskNum = 10;
List<Future> taskFutureList = new ArrayList<>();
for (int i=0; i < taskNum; i++) {
final int taskId = i;
System.out.println("-- submit task " + taskId);
Future future = threadPoolTaskExecutor.submit(() -> {
try {
System.out.printf("task: %s, thread: %s, start at %d\n", taskId, Thread.currentThread().getName(), System.currentTimeMillis()/1000);
Thread.sleep(2_000);
System.out.printf("task: %s, thread: %s, end at %d\n", taskId, Thread.currentThread().getName(), System.currentTimeMillis()/1000);
} catch (InterruptedException e) {
e.printStackTrace();
taskFutureList.add(future);
// 获取任务队列中的任务数量
System.out.println("任务队列任务数量: " + threadPoolTaskExecutor.getThreadPoolExecutor().getQueue().size());
for (Future future : taskFutureList) {
future.get();
threadPoolTaskExecutor.shutdown();