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

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

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

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

比如 : 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 .....
    }
}
天蓬老师
天蓬老师

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

membalas semua(2)
阿神

Ketepikan logiknya.
Tiada penghakiman sama ada semua berbilang benang telah dilaksanakan Tutup selepas gelung sementara selesai. . .
Lepaskan CountDownLatch ke dalam benang melalui pembina

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

Pertanyaan paging bukan serentak (DAO), tetapi pemprosesan data adalah serentak (Perkhidmatan), jadi di manakah tahap transaksi anda ditetapkan?

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan