(六)使用多线程批量数据库千万级别业务数据与mysql数据量大时使用limit分页

在实际业务中,需要从数据库中(千万级别)的数据,批量进行处理,将数据同步到索引服务器中。

核心问题:

1、使用多线程查询数据库中的数据,例如我们有10个线程,每一个线程每次分页步长为10,数据一个10000条。代码如下:

public class Demo18Sequence {

	private static int nThreads = 10;
	private static int pageSize = 10;
	private static int totalNum = 10000;
	
	public static void main(String[] args) {
		try {
			CountDownLatch latch = new CountDownLatch(nThreads);
			ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(nThreads);
			long startTime = System.currentTimeMillis();
			TaskRunnable.start = -pageSize;
			for (int i = 0; i < nThreads; i++) {
				TaskRunnable taski = new TaskRunnable(i+1,pageSize,totalNum,latch);
				newFixedThreadPool.execute(taski);
			}
			latch.await();
			System.out.println(String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - startTime));
			newFixedThreadPool.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值