码迷,mamicode.com
首页 > 数据库 > 详细

多线程并发查询mysql数据库中的数据

时间:2018-11-05 15:17:11      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:而且   服务器   lstat   get   tor   数据库   exec   cto   综合   

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

}

多线程并发查询mysql数据库中的数据

标签:而且   服务器   lstat   get   tor   数据库   exec   cto   综合   

原文地址:https://www.cnblogs.com/herosoft/p/9909099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!