diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index 2355e4c27e..c0639519a9 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -61,6 +61,7 @@ import org.springframework.transaction.annotation.Transactional; import java.text.ParseException; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -188,6 +189,7 @@ public class BaseTaskHubService { /** * 设置任务的报告ID + * * @param tasks 任务集合 */ private void setTaskReportId(List tasks) { @@ -749,27 +751,19 @@ public class BaseTaskHubService { public Map getTaskItemOrder(List taskIdItemIds) { List taskItemIds = getTaskItemByIds(taskIdItemIds); Map> nodeResourceMap = taskItemIds.stream() + .filter(item -> StringUtils.equals(item.getResourceType(), ResourcePoolTypeEnum.NODE.getName())) // 根据条件过滤 .collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); - Map taskItemOrderMap = new HashMap<>(); + List>> futures = nodeResourceMap.keySet().stream() + .filter(StringUtils::isNotBlank) + .map(execTaskItems -> CompletableFuture.supplyAsync(() -> getTaskItemOrder(execTaskItems, taskIdItemIds))) + .toList(); - List threads = new ArrayList<>(); - nodeResourceMap.forEach((node, items) -> { - if (StringUtils.isNotBlank(node)) { - Thread thread = Thread.startVirtualThread(() -> taskItemOrderMap.putAll(getTaskItemOrder(node, taskIdItemIds))); - threads.add(thread); - } - }); - - for (Thread thread : threads) { - try { - thread.join(); - } catch (InterruptedException e) { - LogUtils.error(e); - } - } - - return taskItemOrderMap; + // 等待所有异步任务完成并合并结果 + return futures.stream() + .map(CompletableFuture::join) + .flatMap(map -> map.entrySet().stream()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } public Map getTaskItemOrder(String node, List taskIdItemIds) {