1、数据查询批处理
Lists.partition(Lists.newArrayList(ids), 500).forEach(data ->
entitys.addAll(service.searchEntitysByIds(data))
);
2、异步线程池处理(不考虑响应结果)
@Data
public class AyncData implements Runnable{
private String id;
@SneakyThrows
@Override
public void run() {
//业务处理
service.updateStatus(id);
}
}
@Configuration
public class ThreadConfig {
@Bean("defaultThreadPool")
public TaskExecutor defaultThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数目
executor.setCorePoolSize(10);
// 指定最大线程数
executor.setMaxPoolSize(20);
// 队列中最大的数目
executor.setQueueCapacity(50);
// 线程名称前缀
executor.setThreadNamePrefix("defaultThreadPool_");
// 对拒绝task的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 线程空闲后的最大存活时间
executor.setKeepAliveSeconds(60);
// 加载
executor.initialize();
return executor;
}
}
@Autowired
private TaskExecutor defaultThreadPool;
for (Entitie entitie: Entities) {
AyncData data = new AyncData();
data.setId(entitie.getId());
defaultThreadPool.execute(data);
}
3、异步业务处理(不考虑响应结果)
Thread t = new Thread(){
@Override
public void run() {
//处理业务
}
};
t.start();
4、异步业务处理(考虑响应结果)
大集合对象进行业务处理,异步分批汇总,用到技术:线程编排,集合对象需要线程安全的list(Vector)/map(ConcurrentHashMap)。
//获取线程池
@Autowired
private TaskExecutor defaultThreadPool;
//获取集合分页
private List<Integer> getPageNumbers(int totalPage) {
int pageNumber = 0;
List<Integer> pageNumbers = Lists.newArrayList();
while (pageNumber < totalPage) {
pageNumbers.add(pageNumber);
pageNumber++;
}
return pageNumbers;
}
@Test
public void tutureTest(){
//注意结果对象必须线程安全
List<Entity> infoEntities = new Vector<>();
//大集合分片size
List<List<Entity>> partitions = Lists.partition(Entitys, 5000);
List<Integer> pageNumbers = this.getPageNumbers(partitions.size());
CompletableFuture[] completableFutures = pageNumbers.stream()
.map(pageNumber -> CompletableFuture
.supplyAsync(() -> {
//异步执行业务处理
List<Entity> list = ......;
return !CollectionUtils.isEmpty(list) ? list : Lists.newArrayList();
}, defaultThreadPool)
.whenComplete((list, throwable) -> {
if (!CollectionUtils.isEmpty(list)) {
//处理结果汇总
infoEntities.addAll((Collection<? extends Entity>) list);
}
})).toArray(CompletableFuture[]::new);
//异步执行全部结束(门栓机制)
CompletableFuture.allOf(completableFutures).join();
}