Java批量写入

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

public class BatchTest {

    /**
     * 批量写入
     *
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = reterList();
        int j = 0;
        //批量写入数据量
        int batchNum = 10;
        int batchNum_bak = 10;
        int total = list.size();
        while (true) {
            list.subList(j, j = j + batchNum).forEach(x -> {
                //执行业务逻辑
                System.out.println(x);
            });
            batchNum = ((total - j) % batchNum_bak) == 0 ? batchNum_bak : ((total - j) % batchNum_bak);
            if (j >= total) {
                break;
            }
        }
    }


    /**
     * 造数据
     *
     * @return
     */
    public static List<String> reterList() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 103; i++) {
            list.add(i + "");
        }
        return list;
    }
}

  

  /**
         * 批量处理
         */
        int index = 0;
        int num = 30;
        for (; ; ) {
            list.subList(index, index = index + num).forEach(x -> {
                //执行业务逻辑
                System.out.println(x);
            });
            num = (total - index) < num ? (total - index) : num;
            if (index >= total) {
                break;
            }
        }

 

posted @ 2020-10-25 09:53  洞玄巅峰  阅读(426)  评论(0编辑  收藏  举报