在实际业务中,需要从数据库中(千万级别)的数据,批量进行处理,将数据同步到索引服务器中。
核心问题:
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.