疫情大数据平台可能会出现短时大量访问的情况,就需要线程池进行处理,本篇主要描述手动实现线程池
实现:
首先启动类需要添加这个注解@EnableAsync
@SpringBootApplication
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
然后,新增threadPoolTaskExecutor类,用于创建线程池
@Configuration
@EnableAsync
public class ThreadExecutorConfig {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private int corePoolSize = 20;
private int maxPoolSize = 20;
private int queueCapacity = 20;
@Bean
public Executor threadPoolTaskExecutor(){
System.out.println("创建线程池");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("threadPoolTaskExecutor-》》》");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
executor.initialize();
return executor;
}
使用线程池
任务类
@Component
public class test {
@Async("threadPoolTaskExecutor")
public void testThread() throws InterruptedException {
System.out.println("内部1"+Thread.currentThread().getName());
System.out.println("2s执行方法睡眠前");
Thread.sleep(2000);
System.out.println("2s执行方法睡眠后");
}
@Async("threadPoolTaskExecutor")
public void testThread1() throws InterruptedException {
System.out.println("内部2"+Thread.currentThread().getName());
System.out.println("10s执行方法睡眠前");
Thread.sleep(10000);
System.out.println("10s执行方法睡眠后");
}
}
补充线程池代码
新增一个配置类
public class MyThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
Logger logger = LoggerFactory.getLogger(MyThreadPoolTaskExecutor.class);
@Override
public void execute(Runnable task) {
logThreadPoolStatus();
super.execute(task);
}
@Override
public void execute(Runnable task, long startTimeout) {
logThreadPoolStatus();
super.execute(task, startTimeout);
}
@Override
public Future<?> submit(Runnable task) {
logThreadPoolStatus();
return super.submit(task);
}
@Override
public <T> Future<T> submit(Callable<T> task) {
logThreadPoolStatus();
return super.submit(task);
}
@Override
public ListenableFuture<?> submitListenable(Runnable task) {
logThreadPoolStatus();
return super.submitListenable(task);
}
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
logThreadPoolStatus();
return super.submitListenable(task);
}
private void logThreadPoolStatus() {
logger.info("核心线程数:{}, 最大线程数:{}, 当前线程数: {}, 活跃的线程数: {}",
getCorePoolSize(), getMaxPoolSize(), getPoolSize(), getActiveCount());
}
}
补充bean
@Bean
public Executor myThreadPool() {
int corePoolSize = 5;
int maxPoolSize = 5;
int queueCapacity = 2000;
long keepAliveTime = 30;
String threadNamePrefix = "myThreadPool-->";
RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
throw new RejectedExecutionException("自定义的RejectedExecutionHandler");
}
};
ThreadFactory threadFactory = new ThreadFactory() {
private int i = 1;
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName(threadNamePrefix + i);
i++;
return thread;
}
};
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize,
keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<>(queueCapacity),
threadFactory, rejectedExecutionHandler);
return threadPoolExecutor;
}
@Bean
public Executor myThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new MyThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(5);
threadPoolTaskExecutor.setMaxPoolSize(5);
threadPoolTaskExecutor.setQueueCapacity(2000);
threadPoolTaskExecutor.setThreadNamePrefix("myThreadPoolTaskExecutor-->");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}