//实时气温 String temUrl = tem(dboHourDataVos, temperature, "tem"); //今日最高气温 String temMax = tem(dboHourDataVos, temperature, "temDayMax"); //今日最低气温 String temMin = tem(dboHourDataVos, temperature, "temDayMin"); //近2小时降水 String pre2h = rain(dboHourDataVos, precipitation, "pre2h"); //近6小时降水 String pre6h = rain(dboHourDataVos, precipitation, "pre6h"); //近12小时降水 String pre12h = rain(dboHourDataVos, precipitation, "pre12h"); Map<String, String> map = new HashMap<>(); map.put("temUrl", temUrl); map.put("temMax", temMax); map.put("temMin", temMin); map.put("pre2h", pre2h); map.put("pre6h", pre6h); map.put("pre12h", pre12h); //把上面6个方法改为多线程
时间: 2023-05-17 07:03:21 浏览: 42
可以使用Java的Executor框架来实现多线程,具体实现如下:
1. 创建一个ExecutorService对象,用于管理线程池。
2. 创建一个Callable对象,用于执行每个方法。
3. 将Callable对象添加到一个List中。
4. 调用ExecutorService的invokeAll方法,传入Callable对象的List,返回一个Future对象的List。
5. 遍历Future对象的List,调用get方法获取每个方法的返回值,并将其存入Map中。
代码示例:
```
ExecutorService executor = Executors.newFixedThreadPool(6);
List<Callable<String>> callables = new ArrayList<>();
callables.add(() -> tem(dboHourDataVos, temperature, "tem"));
callables.add(() -> tem(dboHourDataVos, temperature, "temDayMax"));
callables.add(() -> tem(dboHourDataVos, temperature, "temDayMin"));
callables.add(() -> rain(dboHourDataVos, precipitation, "pre2h"));
callables.add(() -> rain(dboHourDataVos, precipitation, "pre6h"));
callables.add(() -> rain(dboHourDataVos, precipitation, "pre12h"));
Map<String, String> map = new HashMap<>();
try {
List<Future<String>> futures = executor.invokeAll(callables);
map.put("temUrl", futures.get(0).get());
map.put("temMax", futures.get(1).get());
map.put("temMin", futures.get(2).get());
map.put("pre2h", futures.get(3).get());
map.put("pre6h", futures.get(4).get());
map.put("pre12h", futures.get(5).get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
executor.shutdown();
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)