1. Common paging types
Traditional: Using the traditional paging method, you can clearly obtain data information, such as how many pieces of data there are, how many pages to display, etc.
Drop-down: Using the drop-down paging method, it is generally impossible to obtain clear information related to the number of data, but after the paging operation, you can still see the previously queried data.
2. Paging query logic
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; } }
3. Data processing logic
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 ..... } }
The above is the detailed content of How to process java multi-threaded data in pages. For more information, please follow other related articles on the PHP Chinese website!