导包
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentlinkedDeque; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; import org.apache.ibatis.session.RowBounds;
代码
– 代码中 DemoTest 为实体类; demoMapper 为dao层接口;demo 为查询的条件参数
// 返回的 list 集合 ListretList = new ArrayList<>(); // 查询符合条件的数据总条数 // 这里自己定义查询的方法和参数 long count = demoMapper.count(); if(count == 0) { // 总条数为 0 ,直接返回 return retList; } // 前端传过来的需要查询的数据条数 // 这里根据自己的业务进行更改 Integer pageSize = 30; // 如果 实际总条数count 比 要查询的总条数pageSize 要小,则取 实际总条数count // 防止查出多余的数据 pageSize = pageSize > count ? (int)count : pageSize; // 每个线程查询的数据条数 // 这里根据自己实际情况来分 Integer partNum = 2000; if(pageSize > partNum) { // 当要查询的总数要大于 partNum 时进行多线程查询 // 最大查询10000条数据 Integer total = pageSize > 10000 ? 10000 : pageSize; // 线程数 Integer threadNum = (int) Math.ceil((double)total/partNum); // 初始化线程池 ExecutorService ex = Executors.newFixedThreadPool(threadNum); // 初始化计数器 CountDownLatch latch = new CountDownLatch(threadNum); // 分段查询汇总 list 集合 ConcurrentlinkedDeque exList = new ConcurrentlinkedDeque<>(); for (int i = 0; i < threadNum; i++) { Integer finalI = i; ex.submit(new Runnable() { @Override public void run() { try { Integer limit = ((finalI +1) * partNum) > total ? (total - (finalI * partNum)) : partNum; System.out.println("===== 线程查询 分页参数: tPreIndex = " + finalI * partNum + " ; limit = " + limit + " ; tNextIndex = " + ((finalI * partNum) + limit) + " ; threadNum = " + threadNum ); RowBounds rowBounds = new RowBounds(finalI * partNum, limit); List rows = demoMapper.pageList(demo,rowBounds); // 汇总数据 synchronized(exList) { exList.addAll(rows); } }catch (Exception e) { e.printStackTrace(); }finally { // 线程结束 latch.countDown(); } } }); } try { // 等待全部查询结束 latch.await(); // 关闭线程 ex.shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } // 排序 此处为倒叙,如果需要正序,将两个参数倒过来就好了 retList = exList.stream().sorted((x,y) -> { return y.getCreateTime().compareTo(x.getCreateTime()); }).collect(Collectors.toList()); } else { // 当要查询的总数要小于等于 partNum 时 直接查询 // 页码 Integer current = 1; RowBounds rowBounds = new RowBounds((current - 1) * pageSize, pageSize); retList = demoMapper.pageList(demo, rowBounds); } return retList;
参考文档
https://blog.csdn.net/u014131617/article/details/109462099
http://www.javashuo.com/article/p-hzbwggki-kp.html
https://www.jianshu.com/p/e233bb37d2e6
https://www.cnblogs.com/wanqieddy/p/3853863.html
https://www.jianshu.com/p/602b3240afaf
https://www.cnblogs.com/nihaofenghao/p/9324562.html



