当前位置:网站首页>Web项目中简单使用线程池
Web项目中简单使用线程池
2022-08-03 18:29:00 【Hejjon】
使用线程池明显提高查询效率.
示例代码如下:
@PostMapping("/list2")
@ApiOperation("使用线程池查询---学生和用户列表")
public ResponseResult<UserVO> listForUserVO2() throws ExecutionException, InterruptedException {
// 1. 创建线程池对象
ThreadPoolExecutor executor = new ThreadPoolExecutor(
5,
10,
15,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(5),
new ThreadPoolExecutor.CallerRunsPolicy()
);
long start = System.currentTimeMillis();
UserVO userVO = new UserVO();
// 2. 异步执行
CompletableFuture<Void> userFuture = CompletableFuture.runAsync(() -> {
// 查询用户
List<BaseUser> baseUserList = baseUserService.list();
userVO.setUserList(baseUserList);
userVO.setUserCount(baseUserList.size());
}, executor);
CompletableFuture<Void> stuFuture = CompletableFuture.runAsync(() -> {
// 查询学生
List<BaseStudent> studentList = baseStudentService.list();
userVO.setStudentList(studentList);
userVO.setStuCount(studentList.size());
}, executor);
// 3. 等待完成
CompletableFuture.allOf(userFuture, stuFuture).get();
long end = System.currentTimeMillis();
long time = end - start;
System.out.println(">>>>>>>>>>>>>>>>>>>>>> 查询耗时: " + time);
// 4. 关闭线程池
executor.shutdown();
return ResponseResult.success(userVO);
}
边栏推荐
猜你喜欢
使用安全浏览器将网页保存为pdf的方法步骤
【白话模电2】二极管特性和分类
Flask框架——项目可安装化
Online monitoring of UPS power supply and operating environment in the computer room, the solution is here
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
Difference差分数组
广告电商、泰山众筹、链动2+1,这3个模式到底怎么样?
爬虫之selenium
Selenium of reptiles
Intelligent security contract - delegatecall (2)
随机推荐
MySQL database account management and optimization
不要小看 WebSocket!长连接、有状态、双向、全双工都是王炸技能
Online monitoring of UPS power supply and operating environment in the computer room, the solution is here
【mysql】SIGN(x) function
ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
ROS仿真环境搭建
【计网】二、物理层
Mock模拟数据,并发起get,post请求(保姆级教程,一定能成功)
超T动力 焕“芯”出发 | 中国重汽专属定制版WP14T产品闪耀登场
PreFixSum前缀和
H.265网页播放器EasyPlayer获取视频流正常,但是播放出现黑屏是什么原因?
201709-3 CCF jason查询 (满分题解)
warnings.warn(“Title is more than 31 characters. Some applications may not be able to read the file
China Hashpower Conference Ascension Kunpeng Ecological Forum was held; Kuaishou established an independent to B business department…
大佬们,flinkcdc 2.2 版本采集sqlserver只能采集到全量的数据,不能采集到增量的数
VsCode preview Geojson data
@resource和@autowired的区别
想要防止数据泄漏,如何选择国产浏览器?
flink-sql 客户端 可以设置并行度 吗?断开算子链
如何成为优秀的产品运营?