springboot多线程
- 新建AsyncTaskConfig,开启@EnableAsync
- 新建IAsyncService接口,及其实现类,新建方法,并开启 @Async
- AsyncService,调用多线程方法
AsyncTaskConfig
@Configuration
@EnableAsync
public class AsyncTaskConfig implements AsyncConfigurer {
// ThredPoolTaskExcutor的处理流程
// 当池子大小小于corePoolSize,就新建线程,并处理请求
// 当池子大小等于corePoolSize,把请求放入workQueue中,池子里的空闲线程就去workQueue中取任务并处理
// 当workQueue放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理
// 当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁
@Override
@Bean
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
//设置核心线程数
threadPool.setCorePoolSize(10);
//设置最大线程数
threadPool.setMaxPoolSize(15);
//线程池所使用的缓冲队列
threadPool.setQueueCapacity(20);
//等待任务在关机时完成--表明等待所有线程执行完
threadPool.setWaitForTasksToCompleteOnShutdown(true);
// 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止
threadPool.setAwaitTerminationSeconds(60);
// 线程名称前缀
threadPool.setThreadNamePrefix("mds-async-task-");
// 初始化线程
threadPool.initialize();
return threadPool;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
// @Bean("doSomethingExecutor")
// public Executor doSomethingExecutor() {
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// // 核心线程数:线程池创建时候初始化的线程数
// executor.setCorePoolSize(10);
// // 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
// executor.setMaxPoolSize(20);
// // 缓冲队列:用来缓冲执行任务的队列
// executor.setQueueCapacity(500);
// // 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
// executor.setKeepAliveSeconds(60);
// // 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
// executor.setThreadNamePrefix("do-something-");
// // 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
// executor.initialize();
// return executor;
// }
}
IAsyncService
public interface IAsyncService {
Future<Long> saveItemNewInfoAsync(Long shopId) ;
}
@Slf4j
@Service
public class AsyncServiceImpl implements IAsyncService {
@Value("${url.}")
private String Url;
@Value("${url..account}")
private String Account;
@Value("${url..appcode}")
private String Apppcode;
@Autowired
private RestTemplate restTemplate;
@Autowired
private ShopMapper ShopMapper;
@Autowired
private ShopNewInfoMapper ShopNewInfoMapper;
@Autowired
private ItemNewInfoMapper ItemNewInfoMapper;
@Async
@Override
public Future<Long> saveItemNewInfoAsync(Long shopId) {
String url=Url+"/item-new-info?account="+Account+"&appcode="+Apppcode;
int pageNo=1;
int pageSize=200;
while (pageNo>0){
String reqUrl=url+"&shopId="+shopId+"&pageNo="+pageNo+"&pageSize="+pageSize;
log.info("开始 2、获取商品昨日最新数据 shopId={},当前页码={},每页数量={}",shopId,pageNo,pageSize);
ParameterizedTypeReference<ItemNewInfoDto> reference = new ParameterizedTypeReference<ItemNewInfoDto>() {};
ResponseEntity<ItemNewInfoDto> entity = restTemplate.exchange(reqUrl, HttpMethod.GET,null,reference);
ItemNewInfoDto rsp = entity.getBody();
List<ItemNewInfoWithBLOBs> rspList=new ArrayList<>();
if( rsp.getResult()==null|| CollectionUtils.isEmpty(rsp.getResult().getResultList())){
pageNo=0;
}else {
pageNo++;
rspList=rsp.getResult().getResultList();
//if(rsp.getResult() instanceof ArrayList<?>){
List<Object> ids= rspList.stream().map(t->t.getId()).collect(Collectors.toList());
Mapper.markFail("_item_new_info","id",ids);
ItemNewInfoMapper.insertBatch(rspList);
}
log.info("结束 2、获取商品昨日最新数据 shopId={},当前页码={},每页数量={},result={}",shopId,pageNo,pageSize,rspList.size());
}
return new AsyncResult<>(shopId);
}
}
ServiceImpl
@Autowired
private IAsyncService AsyncService;
private final Integer SELECT_LIMIT=1000;
/**
* 2、获取商品昨日最新数据
* @return
*/
@Override
public Integer saveItemNewInfo(){
String url=Url+"/item-new-info?account="+Account+"&appcode="+Apppcode;
int count=1;
while (count>0){
List<Shop> findList = this.findShopPage(SELECT_LIMIT,(count-1)*SELECT_LIMIT);
if(findList.size()==0){
count=0;
}else {
count++;
}
Integer threadCount=5;
int times = (int) Math.ceil(findList.size() / 5.0);
for(int i=0;i<times;i++){
List<Long> shopIds= findList.subList(i*threadCount,Math.min ((i+1)*threadCount,findList.size()))
.stream()
.map(t->t.getShopId()).collect(Collectors.toList());
this.doSaveItemNewInfo(shopIds);
}
}
return 1;
}
@SneakyThrows
private Integer doSaveItemNewInfo(List<Long> shopIds){
List<Future> futureList=new ArrayList<>();
for(Long shopId :shopIds){
futureList.add(AsyncService.saveItemNewInfoAsync(shopId)) ;
}
//判断线程池全部执行完毕
for (Future<Long> future : futureList) {
while (true) {
//CPU高速轮询:每个future都并发轮循,判断完成状态然后获取结果,这一行,是本实现方案的精髓所在。即有10个future在高速轮询,完成一个future的获取结果,就关闭一个轮询
if (future.isDone() && !future.isCancelled()) {
//获取future成功完成状态,如果想要限制每个任务的超时时间,取消本行的状态判断+future.get(1000*1, TimeUnit.MILLISECONDS)+catch超时异常使用即可。
Long shopId = future.get();
log.info("2、获取商品昨日最新数据 完成线程任务 ,shopId=" + future.get());
// futureList.remove(future);
break;//当前future获取结果完毕,跳出while
} else {
//每次轮询休息1毫秒(CPU纳秒级),避免CPU高速轮循耗空CPU---》新手别忘记这个
Thread.sleep(1);
}
}
}
return 1;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。