spring注解@EnableAsync@Async实现多线程
通过spring给我们提供的ThreadPoolTaskExecutor就可以使,例子如下:
首先定义配置类
package com.andong.spring.test;
import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor;
@Configuration
@ComponentScan("com.andong.test")
@EnableAsync // 启用异步任务
public class ThreadConfig {
@Bean // 执行需要依赖线程池,配置一个线程池
public Executor getExecutor() {
Thread