From 72339c1f9c9d7f71c684d159ebf81cb022449cdf Mon Sep 17 00:00:00 2001 From: wangliwen Date: Sat, 10 Dec 2022 16:45:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E5=A4=84=E7=90=86=E5=BE=85?= =?UTF-8?q?=E5=8A=9E=E4=BB=BB=E5=8A=A1=E5=86=85=E8=AF=A6=E6=83=85=EF=BC=8C?= =?UTF-8?q?=E5=A4=A7=E5=B9=85=E5=87=8F=E5=B0=91=E8=AE=A1=E7=AE=97=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activiti/service/ActTaskService.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/renren-admin/src/main/java/io/renren/modules/activiti/service/ActTaskService.java b/renren-admin/src/main/java/io/renren/modules/activiti/service/ActTaskService.java index b562bd0b..024f9c06 100644 --- a/renren-admin/src/main/java/io/renren/modules/activiti/service/ActTaskService.java +++ b/renren-admin/src/main/java/io/renren/modules/activiti/service/ActTaskService.java @@ -65,10 +65,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.*; import java.util.stream.Collectors; /** @@ -82,7 +79,7 @@ public class ActTaskService extends BaseServiceImpl { private static final ObjectMapper oMapper = new ObjectMapper(); private static final Integer CPUNUM = Runtime.getRuntime().availableProcessors(); - private static final ExecutorService executor = Executors.newFixedThreadPool(CPUNUM); + private static final ExecutorService executor = Executors.newWorkStealingPool(CPUNUM * 2); public static String Task_HANDLE_STATE = "handleState"; //任务节点处理状态key public static String Task_HANDLE_STATE_AGREE = "agree"; //同意 @@ -193,11 +190,21 @@ public class ActTaskService extends BaseServiceImpl { } List tasks = nativeTaskQuery.listPage((curPage - 1) * limit, limit); long count = nativeCountTaskQuery.count(); + List completableFutures = new ArrayList(); List list_ = tasks.stream().map(index -> { TaskQuery taskQuery = taskService.createTaskQuery(); - Task temp = taskQuery.taskId(index.getId()).includeProcessVariables().singleResult(); + CompletableFuture tempTask = CompletableFuture.supplyAsync(() -> taskQuery.taskId(index.getId()).includeProcessVariables().singleResult(), executor); + Task temp = null; + try { + temp = (Task) tempTask.get(); + } catch (InterruptedException e) { + logger.error("异步获取失败", e); + } catch (ExecutionException e) { + logger.error("异步获取失败", e); + } return temp; - }).collect(Collectors.toList()); + }).filter(Objects::nonNull).collect(Collectors.toList()); + CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()])).join(); return new PageData<>(tasks2TaskDtos(list_), (int) count); // 以下注释为原实现方案 // TaskQuery taskQuery = taskService.createTaskQuery();