2.1 Spring boot/cloud 线程池
程序员文章站
2022-05-01 13:04:05
...
Step 1:ExecutePool配置,开启@EnableAsync支持异步任务
package com.springboot.begin.threadPool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ExecutorPoolConfig {
private int corePoolSize = 10;
private int maxPoolSize = 50;
private int queueSize = 10;
private int keepAlive = 60;
@Bean
public Executor testExecutorPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueSize);
executor.setKeepAliveSeconds(keepAlive);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
executor.setThreadNamePrefix("TestExecutorPool-");
executor.initialize();
return executor;
}
}
Step 2:执行线程,声明一个异步任务
package com.springboot.begin.threadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class ThreadPoolUser {
@Async("testExecutorPool")
public void test(){
String info = "test---" + Thread.currentThread().getName();
log.info(info);
}
}
Step 3:测试 (接口调用)
package com.springboot.begin;
import com.springboot.begin.threadPool.ThreadPoolUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private ThreadPoolUser threadPoolUser;
@RequestMapping(value = "/threadpool/test" , method=RequestMethod.GET)
public String threadPoolTest() {
threadPoolUser.test();
return "ok";
}
}
上一篇: Spring Batch 注册监听器
下一篇: hive的分桶表
推荐阅读
-
JSP spring boot / cloud 使用filter防止XSS
-
详解Spring Boot Mysql 版本驱动连接池方案选择
-
详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失
-
spring boot使用自定义的线程池执行Async任务
-
2.1spring cloud 环境配置
-
Spring Boot 1.5.* 升级 2.1 - 完善中
-
字节跳动Java研发面试99题(含答案):JVM+Spring+MySQL+线程池+锁
-
spring Boot+spring Cloud实现微服务详细教程第二篇
-
一起学习Spring boot 2.1.X | 第五篇:Mybatis Druid 数据库(注解版)
-
JSP spring boot / cloud 使用filter防止XSS