List<Thread> threadList = strList.stream()
.filter(StringUtils::isNotBlank)
.map(e -> new Thread(() ->
termService.getResult(e,startDate, endDate)))
.collect(Collectors.toList());
threadList.forEach(Thread::start);
threadList.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
throw new CommonException("多线程查询时出错", e);
}
});
strList 的size() 决定启动的线程数,strList中的每个元素都作为参数e传入termService的getResult方法中,可以在getResult方法中将数据库的查询结果放入缓存,方便后续使用。
并行查询与线程管理
这段代码展示了如何根据`strList`的大小创建相应数量的线程,每个线程执行`termService.getResult()`方法,该方法可能将数据库查询结果放入缓存。线程启动后,使用`join()`确保所有线程执行完毕,如果出现InterruptedException,则抛出自定义异常。
1362

被折叠的 条评论
为什么被折叠?



