批处理数据

多线程处理:
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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值