学习笔记
- 一、ThreadPoolTaskExecutor与ThreadPoolExecutor的区别
- 二、编写配置文件ThreadPoolConfig
- 二、编写Controller
- 三、编写Service
- 3.1、注解
- 3.1、注入
一、ThreadPoolTaskExecutor与ThreadPoolExecutor的区别
ThreadPoolExecutor 是JDK自1.5添加的线程池。
ThreadPoolTaskExecutor是Spring的线程池【本文使用的】。
Spring线程池ThreadPoolTaskExecutor其实是对JDK线程池ThreadPoolExecutor的一个封装。
二、编写配置文件ThreadPoolConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.*;
@Configuration
public class ThreadPoolConfig {
/** 核心线程数(默认线程数) */
private static final int CORE_POOL_SIZE = 20;
/** 最大线程数 */
private static final int MAX_POOL_SIZE = 50;
/** 允许线程空闲时间(单位:默认为秒) */
private static final int KEEP_ALIVE_TIME = 10;
/** 缓冲队列大小 */
private static final int QUEUE_CAPACITY = 200;
/** 线程池名前缀 */
private static final String THREAD_NAME_PREFIX = "ThreadPool-";
@Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
public ThreadPoolTaskExecutor taskExecutor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setMaxPoolSize(MAX_POOL_SIZE);
executor.setQueueCapacity(QUEUE_CAPACITY);
executor.setKeepAliveSeconds(KEEP_ALIVE_TIME);
executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
// 初始化
executor.initialize();
return executor;
}
}
二、编写Controller
import com.ynx.exarejuc.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloController {
@Resource
private HelloService helloService;
@RequestMapping("/hello100")
public void sayHello100() throws InterruptedException {
for (int i = 1; i <100 ; i++) {
helloService.hello();
}
}
}
三、编写Service
3.1、注解
在使用多线程方法上标注@Async时表明调用的线程池 同时在类上和application类上加上@EnableAsync
通过这个@Async注解的方式去异步调用的
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.Executor;
@EnableAsync
@Service
public class HelloService {
//在使用多线程方法上标注@Async时表明调用的线程池 同时在类上和application类上加上@EnableAsync,如下
@Async("taskExecutor")
public void hello() throws InterruptedException {
System.out.println(Thread.currentThread().getName() + "处理任务ing...");
}
}
访问http://localhost:8080/hello100
3.1、注入
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.Executor;
@Service
public class HelloService {
@Resource(name = "taskExecutor")
private Executor taskExecutor;
public void hello(){
taskExecutor.execute(()->{
System.out.println(Thread.currentThread().getName() + "处理任务ing...");
});
}
}
访问http://localhost:8080/hello100