List<PortMonitor> portMonitorList = portMonitorService.selectPortMonitorList(pm); if (portMonitorList.size() > 0) {有必要加portMonitorList!=null吗
时间: 2024-01-25 08:05:15 浏览: 22
非常感谢您的提问。在这段代码中,如果 `portMonitorList` 是 null,那么在调用 `portMonitorList.size()` 方法时会出现空指针异常。因此,为了避免这种情况,最好在判断 `portMonitorList.size() > 0` 前,先判断 `portMonitorList` 是否为 null。具体修改如下:
```
List<PortMonitor> portMonitorList = portMonitorService.selectPortMonitorList(pm);
if (portMonitorList != null && portMonitorList.size() > 0) {
for (PortMonitor portMonitor: portMonitorList) {
// ...
}
portMonitorService.batchUpdateStatus(portMonitorList);
}
```
这样,在判断 `portMonitorList.size() > 0` 前,会先进行非空判断,避免了出现空指针异常。
相关问题
为以下代码加非空判断: PortMonitor pm = new PortMonitor(); List<PortMonitor> portMonitorList = portMonitorService.selectPortMonitorList(pm); for (PortMonitor portMonitor: portMonitorList) { String ip = portMonitor.getIp(); Integer port = portMonitor.getPort(); boolean portStatus = portMonitorService.isPortOpen(ip, port); portMonitor.setStatus(portStatus ? "0" : "1"); } portMonitorService.batchUpdateStatus(portMonitorList);
好的,为了加非空判断,可以在使用 `portMonitorService.selectPortMonitorList(pm)` 方法前,先判断 `portMonitorService` 是否为 null,并且在使用 `portMonitor.getIp()` 和 `portMonitor.getPort()` 方法前,先判断 `portMonitor` 是否为 null。具体修改如下:
```
if(portMonitorService != null) {
PortMonitor pm = new PortMonitor();
List<PortMonitor> portMonitorList = portMonitorService.selectPortMonitorList(pm);
if(portMonitorList != null) {
for (PortMonitor portMonitor: portMonitorList) {
if(portMonitor != null) {
String ip = portMonitor.getIp();
Integer port = portMonitor.getPort();
boolean portStatus = portMonitorService.isPortOpen(ip, port);
portMonitor.setStatus(portStatus ? "0" : "1");
}
}
portMonitorService.batchUpdateStatus(portMonitorList);
}
}
```
这样,在代码执行前会先进行非空判断,避免出现空指针异常。
// 分成 10 个线程并行处理数据 int threadNum = 10; int dataSizePerThread = awardIds.size() / threadNum; List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>(); for (int i = 0; i < threadNum; i++) { int startIndex = i * dataSizePerThread; int endIndex = (i == threadNum - 1) ? awardIds.size() : (i + 1) * dataSizePerThread; List<Long> subList = awardIds.subList(startIndex, endIndex); CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> { List<TCaAwardInfo> result = new ArrayList<>(); List<TCaAwardInfo> tCaAwardInfos = awardApiService.queryActivationCode(subList); result.addAll(tCaAwardInfos); return result; }, taskExecutor); futures.add(future); } // 等待所有线程执行完毕 CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); allFutures.join(); // 合并结果 List<TCaAwardInfo> activationCodeList = futures.stream() .flatMap(future -> future.join().stream()) .collect(Collectors.toList()); 优化一下这个代码
这段代码的并行处理数据的逻辑已经很好了,可以考虑在结果合并的时候进行优化。可以使用`CompletableFuture.join()`方法替代`CompletableFuture.join().stream()`,这样可以避免创建Stream对象,提高代码执行效率。同时,可以考虑使用`addAll()`方法替代`flatMap()`和`collect(Collectors.toList())`方法,从而避免创建Stream对象和List对象,提高代码执行效率。代码改进后如下:
```
int threadNum = 10;
int dataSizePerThread = awardIds.size() / threadNum;
List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
int startIndex = i * dataSizePerThread;
int endIndex = (i == threadNum - 1) ? awardIds.size() : (i + 1) * dataSizePerThread;
List<Long> subList = awardIds.subList(startIndex, endIndex);
CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> {
List<TCaAwardInfo> result = new ArrayList<>();
List<TCaAwardInfo> tCaAwardInfos = awardApiService.queryActivationCode(subList);
result.addAll(tCaAwardInfos);
return result;
}, taskExecutor);
futures.add(future);
}
List<TCaAwardInfo> activationCodeList = new ArrayList<>();
for (CompletableFuture<List<TCaAwardInfo>> future : futures) {
activationCodeList.addAll(future.join());
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)