快捷搜索: 王者荣耀 脱发

使用forkjoin框架分页查询所有数据的例子

使用forkjoin框架分页查询所有数据的例子

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

@RestController
@RequestMapping("/test2")
@PropertySource("classpath:/application.properties")
@Slf4j
public class ForkJoinPollController {
          
   

    @Autowired
    private SyncPersonInfoServiceImp syncPersonInfoService;

    @ApiOperation("测试forkJoinPoll")
    @GetMapping("/forkJoinPoll")
    public void forkJoinPoll(){
          
   
        log.info("forkJoinPoll start...");
        ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors() * 2);
        // 总页数,提前查询出来,数据量不大就不用拆任务
        int total = syncPersonInfoService.getTotal();
        int totalPage = (int)Math.ceil(total * 1.0 / IrdsConstant.PAGE_SIZE_MAX);
        SumTask innerFind = new SumTask(1, totalPage);
        pool.invoke(innerFind);
        List<VChrHkws> join = innerFind.join();
        pool.shutdown();
        log.info("forkJoinPoll end,size={}",join.size());
    }


    @AllArgsConstructor
    public class SumTask extends RecursiveTask<List<VChrHkws>> {
          
   

        private final Integer fromIndex;
        private final Integer toIndex;

        @Override
        protected List<VChrHkws> compute() {
          
   
            // 如果任务足够小就触发任务
            if (toIndex - fromIndex < 2) {
          
   
                List<VChrHkws> list = new ArrayList<>();
                for (int pageNo = fromIndex; pageNo <= toIndex; pageNo++) {
          
   
                    ApiPageListData<VChrHkws> pageData = syncPersonInfoService.getPageListData(pageNo, IrdsConstant.PAGE_SIZE_MAX);
                    // 查询数据库返回值
                    list.addAll(pageData.getList());
                }
                return list;
            } else {
          
   
                // 如果任务大于阈值,就分裂成两个子任务计算
                int mid = (fromIndex + toIndex) / 2;
                SumTask left = new SumTask(fromIndex, mid);
                SumTask right = new SumTask(mid + 1, toIndex);
                invokeAll(left, right);
                List<VChrHkws> join = left.join();
                join.addAll(right.join());
                return join;
            }
        }
    }
}
经验分享 程序员 微信小程序 职场和发展