public LinkedHashMap<String, Object> getDistinctData(senseDataDto senseDataDto) { LinkedHashMap<String, Object> distinctData = new LinkedHashMap<>(); distinctData.put("pin35", senseDataMapper.getDistinctPin35(senseDataDto)); distinctData.put("pin36", senseDataMapper.getDistinctPin36(senseDataDto)); distinctData.put("pin29", senseDataMapper.getDistinctPin29(senseDataDto)); distinctData.put("pin30", senseDataMapper.getDistinctPin30(senseDataDto)); distinctData.put("pin31", senseDataMapper.getDistinctPin31(senseDataDto)); distinctData.put("pin1", senseDataMapper.getDistinctPin1(senseDataDto)); distinctData.put("pin2", senseDataMapper.getDistinctPin2(senseDataDto)); distinctData.put("pin3", senseDataMapper.getDistinctPin3(senseDataDto)); distinctData.put("pin4", senseDataMapper.getDistinctPin4(senseDataDto)); return distinctData; },优化这个方法使其异步加载,并在Controller层返回distinctData数据给前端,使得加载的速度快一点
时间: 2024-04-14 21:31:17 浏览: 114
HashMap<K,V>对象-java语言中使用的类总结
要将该方法改为异步加载,你可以使用`CompletableFuture`来实现。首先,将方法的返回类型更改为`CompletableFuture<LinkedHashMap<String, Object>>`。然后,在方法内部,使用`CompletableFuture.supplyAsync()`方法来异步执行各个数据库查询操作。最后,使用`CompletableFuture.allOf()`方法等待所有异步操作完成,并返回结果。
下面是修改后的代码示例:
```java
public CompletableFuture<LinkedHashMap<String, Object>> getDistinctDataAsync(senseDataDto senseDataDto) {
CompletableFuture<Object> pin35Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin35(senseDataDto));
CompletableFuture<Object> pin36Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin36(senseDataDto));
CompletableFuture<Object> pin29Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin29(senseDataDto));
CompletableFuture<Object> pin30Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin30(senseDataDto));
CompletableFuture<Object> pin31Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin31(senseDataDto));
CompletableFuture<Object> pin1Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin1(senseDataDto));
CompletableFuture<Object> pin2Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin2(senseDataDto));
CompletableFuture<Object> pin3Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin3(senseDataDto));
CompletableFuture<Object> pin4Future = CompletableFuture.supplyAsync(() -> senseDataMapper.getDistinctPin4(senseDataDto));
return CompletableFuture.allOf(pin35Future, pin36Future, pin29Future, pin30Future, pin31Future, pin1Future, pin2Future, pin3Future, pin4Future)
.thenApply(v -> {
LinkedHashMap<String, Object> distinctData = new LinkedHashMap<>();
distinctData.put("pin35", pin35Future.join());
distinctData.put("pin36", pin36Future.join());
distinctData.put("pin29", pin29Future.join());
distinctData.put("pin30", pin30Future.join());
distinctData.put("pin31", pin31Future.join());
distinctData.put("pin1", pin1Future.join());
distinctData.put("pin2", pin2Future.join());
distinctData.put("pin3", pin3Future.join());
distinctData.put("pin4", pin4Future.join());
return distinctData;
});
}
```
在Controller层,你可以调用`getDistinctDataAsync()`方法,并使用`CompletableFuture`的`join()`方法来等待异步操作的完成,然后将结果返回给前端。
注意:异步加载并不一定会使加载速度变快,具体的性能还取决于数据库查询操作的耗时和服务器的处理能力。
阅读全文