java - 分页查询中如何使用多线程加快处理速度?
天蓬老师
天蓬老师 2017-04-18 10:54:05
0
2
1370

一个历史表中有大量的数据,现在要通过分页式查询处理转换数据。

现在将处理数据的逻辑放在线程池中处理,以加快处理流程。

可是总是出现事务方面的异常

比如 : SQLNonTransientConnectionException

请问该如何解决上述异常,或者有什么好的多线程分页查询处理方案


原来问题描述不太清楚,现在添加以下代码(手敲,如果有错,请多包含)

分页式查询逻辑:

int pageSize = 100;
int currentPageLength = 0;
int pageIndex = 0;
ExecutorService exe  = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
do {
    int offset = pageIndex * pageSize;
    List<TradeInfo> tradeInfos = tradeInfoService.findTradeInfoBysPage(queryParams,offset,pageSize);
    if (null != tradeInfos && tradeInfos.size() > 0) {
        currentPageLength = tradeInfos.size();
        TradeInfoProcesserTask task = new TradeInfoProcesserTask(tradeInfos );
        exe.execute(task);
        pageIndex++;
    }else{
        System.out.println("Page Query TradeInfo Got NOTHING! Break query loop!");
        break;
    }
} while (currentPageLength == pageSize);

exe.shutdown();

while(true) {
    if(exe.isTerminated()){
        doOtherThings();
        System.out.println("分页式多线程处理数据完毕!");
        break;
    }
}

数据处理逻辑:

public class TradeInfoProcesserTask implements Runnable{
    private volatile List<TradeInfo> tradeInfos;
    
    public TradeInfoProcesserTask (List<TradeInfo> _tradeInfos){
        tradeInfos = _tradeInfos;
    }
    
    @Override
    public void run() {
        processTradeInfos();
    }
    
    private void processTradeInfos(){
        //do something with tradeInfos .....
    }
}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
阿神

Let’s not talk about logic.
Now there is no judgment whether all multi-threads have been executed. The while loop will be shut down. . .
Pass CountDownLatch into the thread through the constructor

ExecutorService exe  = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
CountDownLatch latch = new CountDownLatch(?); //?代表开启全部线程的数量
do {
    int offset = pageIndex * pageSize;
    List<TradeInfo> tradeInfos = tradeInfoService.findTradeInfoBysPage(queryParams,offset,pageSize);
    if (null != tradeInfos && tradeInfos.size() > 0) {
        currentPageLength = tradeInfos.size();
        TradeInfoProcesserTask task = new TradeInfoProcesserTask(tradeInfos, latch);
        exe.execute(task);
        pageIndex++;
    }else{
        System.out.println("Page Query TradeInfo Got NOTHING! Break query loop!");
        break;
    }
} while (currentPageLength == pageSize);

latch.await(); //多线程全部执行完
exe.shutdown();
doOtherThings();
System.out.println("分页式多线程处理数据完毕!");

public class TradeInfoProcesserTask implements Runnable{
    private volatile List<TradeInfo> tradeInfos;
    private CountDownLatch latch;
    
    public TradeInfoProcesserTask (List<TradeInfo> _tradeInfos, CountDownLatch latch){
        tradeInfos = _tradeInfos;
        this.latch = latch;
    }
    
    @Override
    public void run() {
        processTradeInfos();
        latch.countDown();
    }
    
    private void processTradeInfos(){
        //do something with tradeInfos .....
    }
}
Ty80

Paging query is not concurrent (DAO), data processing is concurrent (Service), so where is your transaction level set?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template