这里只讲如何应用
- 启动类加入
language
- 配置文件加入
#线程池配置参数
executor.corePoolSize: 1
executor.maxPoolSize: 1
executor.queueCapacity: 0
executor.threadNamePrefix: SearchExecutor-
- 初始化自定义线程
MyExecutorPool.class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class MyExecutorPool {
@Autowired
private ThreadPoolProperties threadPoolProperties;
@Bean(name = "myAsyncThread")
public ThreadPoolTaskExecutor myTaskAsyncPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//表示线程池核心线程,正常情况下开启的线程数量。
executor.setCorePoolSize(threadPoolProperties.getCorePoolSize());
//当核心线程都在跑任务,还有多余的任务会存到此处。
executor.setMaxPoolSize(threadPoolProperties.getMaxPoolSize());
//如果queueCapacity存满了,还有任务就会启动更多的线程,直到线程数达到maxPoolSize。如果还有任务,则根据拒绝策略进行处理。
executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());
//该策略是抛弃。
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
//非核心线程的超时时长,超长后会被回收。
executor.setKeepAliveSeconds(0);
executor.setThreadNamePrefix(threadPoolProperties.getThreadNamePrefix());
//初始化线程池。
executor.initialize();
return executor;
}
}
ThreadPoolProperties.class
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties("executor")
@Component
public class ThreadPoolProperties {
private Integer corePoolSize;
private Integer maxPoolSize;
private Integer queueCapacity;
private String threadNamePrefix;
public Integer getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
public Integer getMaxPoolSize() {
return maxPoolSize;
}
public void setMaxPoolSize(Integer maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public Integer getQueueCapacity() {
return queueCapacity;
}
public void setQueueCapacity(Integer queueCapacity) {
this.queueCapacity = queueCapacity;
}
public String getThreadNamePrefix() {
return threadNamePrefix;
}
public void setThreadNamePrefix(String threadNamePrefix) {
this.threadNamePrefix = threadNamePrefix;
}
}
- 测试
controller
import com.example.demo.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class MyTestController {
@Autowired
private SearchService searchService;
@GetMapping("/search")
public Boolean sayHello(){
searchService.executeAsync();
return true;
}
}
service
@Service
public class SearchService {
@Async("myAsyncThread") //指定使用那个线程池配置,不然会使用spring默认的线程池
public void executeAsync() {
System.out.println("start executeAsync");
try {
System.out.println("当前运行的线程名称:" + Thread.currentThread().getName());
Thread.sleep(10000);
throw new Exception("线程中报错了");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end executeAsync");
}
}
Q.E.D.