用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;
}