多線程並發查詢mysql數據庫中的數據


用10個一次拉2噸的卡車代替1個一次拉10噸的卡車。前提是有資源折騰,比如線程池,多核cpu,也要考慮線程的切換代價。把java服務器和數據庫服務器綜合利用起來,傳統的方式是java服務器發送一條指令給數據庫就坐等喝茶拿結果,數據庫累個半死才出結果,而且出力不討好,嫌干活慢,現在也要讓java服務器也要干點事,這樣大家都心里比較平衡點。

List<CompletableFuture<List<TimesAndAmount>>> allStationsTimesAmount =
inputParamArrayList.stream()
.map(inputParam -> CompletableFuture.supplyAsync(() ->nonOilSalesAndPerCustomerTransactionDao.getTimesHoursInterval(inputParam), executorService))
.collect(Collectors.toList());

List<List<TimesAndAmount>> timesAmount = allStationsTimesAmount.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());


private List<String> getBarcodeList(String[] deptIds, String[] ids) {

List<String> list = new ArrayList<>();
List<String> list1 = new ArrayList<>();
if (deptIds != null){
list = Arrays.asList(deptIds);

List<CompletableFuture<List<String>>> allBarcodes =
list.stream()
.map(inputParam -> CompletableFuture.supplyAsync(() ->nonOilSalesAndPerCustomerTransactionDao.getBarcodesBydeptid(inputParam), executorService))
.collect(Collectors.toList());

List<List<String>> listList = allBarcodes.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());

//List<List<String>> 轉換為List<String> ,使用flatMap

list1 =
listList.stream()
.flatMap(inner -> inner.stream()).collect(Collectors.toList());


}

if (ids != null){
list1.addAll(Arrays.asList(ids));
}

return list1;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM