多线程处理:
executor.submit(new BatchOptionData(t));
@Slf4j
public class BatchOptionData implements Callable<Boolean> {
/**
* 批处理数量
*/
private int num = 50;
/**
* 数据
*/
private List<T> t;
/**线程池*/
private ThreadPoolExecutor threadPoolExecutor =
new ThreadPoolExecutor(2*Runtime.getRuntime().availableProcessors(),
5*Runtime.getRuntime().availableProcessors(),
30,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy()
);
/**
* 构造器
* @param t
*/
public BatchOptionDownloadFile(List<T> t) {
this.t = t;
}
@Override
public Boolean call() throws Exception {
try {
batchOP(t);
return true;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 批处理
* @param t
*/
private synchronized void batchOP(List<T> t) {
try {
if (CollectionUtils.isNotEmpty(t)){
log.info("开始处理");
int size = monitorImgUrl.size();
if (size<=num){
//处理逻辑
}else {
batchOpSpilit(t,num);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* list分割
* @param t
* @param num
*/
private void batchOpSpilit(List<T> t, int num) {
List<List<T>> lists = pagingList(t, num);
try {
for (List<T> t : lists) {
threadPoolExecutor.execute(()->{
log.info("超过50,批处理");
batchOP(t);
});
}
}catch (Exception e) {
e.printStackTrace();
}finally {
threadPoolExecutor.shutdown();
}
}
/**
* 分割方法
* @param list
* @param pageSize
* @param <T>
* @return
*/
public static <T> List<List<T>> pagingList(List<T> list, int pageSize){
int length = list.size();
int num = (length+pageSize-1)/pageSize;
List<List<T>> newList = new ArrayList<>();
for(int i=0;i<num;i++){
int fromIndex = i*pageSize;
int toIndex = (i+1)*pageSize<length?(i+1)*pageSize:length;
newList.add(list.subList(fromIndex,toIndex));
}
return newList;
}
}