1.自定义线程池
@EnableAsync
@Component
public class TaskPoolConfig {
/**
* 自定义线程池
*/
@Bean("taskExecutor")
public Executor taskExecutor() {
// 返回可用处理器的Java虚拟机的数量 12
int i = Runtime.getRuntime().availableProcessors();
System.out.println("系统最大线程数 : " + i);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程池大小
executor.setCorePoolSize(16);
// 最大线程数
executor.setMaxPoolSize(20);
// 配置队列容量,默认值为Integer.MAX_VALUE
executor.setQueueCapacity(99999);
// 活跃时间
executor.setKeepAliveSeconds(60);
// 线程名字前缀
executor.setThreadNamePrefix("asyncServiceExecutor -");
// 设置此执行程序应该在关闭时阻止的最大秒数,以便在容器的其余部分继续关闭之前等待剩余的任务完成他们的执行
executor.setAwaitTerminationSeconds(60);
// 等待所有的任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
2. 创建异步接口
public interface AsyncService {
void checkSignStatus(String id, int type) throws Exception;
}
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
@Async("taskExecutor")
@Override
public void checkSignStatus(String contractId, int type) throws Exception {
String name = Thread.currentThread().getName();
log.info("currentThreadName={}",name);
//业务逻辑
}
}
3.直接使用接口
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {
@Autowired
AsyncService asyncService;
@RequestMapping(value = "/testAsync",method = RequestMethod.GET)
public Result<Object> testAsync() throws Exception {
asyncService.checkSignStatus("test123",12);
System.out.println("currentThreadName:");
System.out.println(Thread.currentThread().getName());
return Result.success();
}
}