亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - 分頁(yè)查詢(xún)中如何使用多線程加快處理速度?
天蓬老師
天蓬老師 2017-04-18 10:54:05
0
2
1643

一個(gè)歷史表中有大量的數(shù)據(jù),現(xiàn)在要通過(guò)分頁(yè)式查詢(xún)處理轉(zhuǎn)換數(shù)據(jù)。

現(xiàn)在將處理數(shù)據(jù)的邏輯放在線程池中處理,以加快處理流程。

可是總是出現(xiàn)事務(wù)方面的異常

比如 : SQLNonTransientConnectionException

請(qǐng)問(wèn)該如何解決上述異常,或者有什么好的多線程分頁(yè)查詢(xún)處理方案?


原來(lái)問(wèn)題描述不太清楚,現(xiàn)在添加以下代碼(手敲,如果有錯(cuò),請(qǐng)多包含)

分頁(yè)式查詢(xún)邏輯:

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("分頁(yè)式多線程處理數(shù)據(jù)完畢!");
        break;
    }
}

數(shù)據(jù)處理邏輯:

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 .....
    }
}
天蓬老師
天蓬老師

歡迎選擇我的課程,讓我們一起見(jiàn)證您的進(jìn)步~~

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(?); //?代表開(kāi)啟全部線程的數(shù)量
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(); //多線程全部執(zhí)行完
exe.shutdown();
doOtherThings();
System.out.println("分頁(yè)式多線程處理數(shù)據(jù)完畢!");

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