Java 线程分段查询数据,再整合排序

4 篇文章 0 订阅

Java 线程分段查询数据,再整合排序

笔记

导包

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 集合
List<DemoTest> retList = 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<DemoTest> 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<DemoTest> 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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值