From 5d131a1d375baaa757c9413b81195e64560e2413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=B3=B0?= <9138985+zhao-feng11@user.noreply.gitee.com> Date: Sat, 8 Feb 2025 12:03:08 +0000 Subject: [PATCH 1/2] =?UTF-8?q?add=20=E5=9F=BA=E4=BA=8Eissues=E6=8B=93?= =?UTF-8?q?=E5=B1=95.txt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 赵峰 <9138985+zhao-feng11@user.noreply.gitee.com> --- ...272\216issues\346\213\223\345\261\225.txt" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "\345\237\272\344\272\216issues\346\213\223\345\261\225.txt" diff --git "a/\345\237\272\344\272\216issues\346\213\223\345\261\225.txt" "b/\345\237\272\344\272\216issues\346\213\223\345\261\225.txt" new file mode 100644 index 0000000..6b76af7 --- /dev/null +++ "b/\345\237\272\344\272\216issues\346\213\223\345\261\225.txt" @@ -0,0 +1,80 @@ +https://gitee.com/jd-platform-opensource/asyncTool/issues/I4HLQE?from=project-issue +浩宇星眸 +3年前 +想灵活的控制线程池的使用,修改Async类里面的部分代码: + +public class Async { + /** + * 默认不定长线程池 + */ + private static final ThreadPoolExecutor COMMON_POOL = (ThreadPoolExecutor) Executors.newCachedThreadPool(); + /** + * 注意,这里是个static,也就是只能有一个线程池。用户自定义线程池时,也只能定义一个 + */ + //private static ExecutorService executorService; + + @SuppressWarnings("unchecked") + public static CompletableFuture[] newCompletableFutureArray(int size) { + return new CompletableFuture[size]; + } + + public static ExecutorService newExecutorService(int nThreads) { + return Executors.newFixedThreadPool(nThreads); + } + + /** + * 出发点 + */ + public static boolean beginWork(long timeout, ExecutorService customExecutorService , List workerWrappers) throws ExecutionException, InterruptedException { + if(workerWrappers == null || workerWrappers.size() == 0) { + return false; + } + ExecutorService executorService= Optional.ofNullable(customExecutorService ).orElse(COMMON_POOL); + //定义一个map,存放所有的wrapper,key为wrapper的唯一id,value是该wrapper,可以从value中获取wrapper的result + Map forParamUseWrappers = new ConcurrentHashMap<>(); + CompletableFuture[] futures = newCompletableFutureArray(workerWrappers.size()); +... + /** + * 异步执行,直到所有都完成,或失败后,发起回调 + */ + public static void beginWorkAsync(long timeout, ExecutorService customExecutorService, IGroupCallback groupCallback, + WorkerWrapper... workerWrapper) { + if (groupCallback == null) { + groupCallback = new DefaultGroupCallback(); + } + IGroupCallback finalGroupCallback = groupCallback; + // 线程池变量,若为空使用默认线程池 + ExecutorService executorService = Optional.ofNullable(customExecutorService).orElse(COMMON_POOL); + executorService.submit(() -> { + try { + boolean success = beginWork(timeout, executorService, workerWrapper); + if (success) { + finalGroupCallback.success(workerWrapper); + } else { + finalGroupCallback.failure(new TimeoutException(), workerWrapper); + } + } catch (ExecutionException | InterruptedException e) { + e.printStackTrace(); + finalGroupCallback.failure(e, workerWrapper); + } + }); + } +... + /** + * 关闭线程池 + */ + public static void shutDown() { + shutDown(COMMON_POOL); + } + + /** + * 关闭线程池 + */ + public static void shutDown(ExecutorService executorService) { + if (executorService != null) { + executorService.shutdown(); + } + } + +使用示例: + -- Gitee From dc3a3c2cfc693dd90f1781b75c3b17f43355bf44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=B3=B0?= <9138985+zhao-feng11@user.noreply.gitee.com> Date: Sat, 8 Feb 2025 12:25:21 +0000 Subject: [PATCH 2/2] =?UTF-8?q?add=20=E6=B5=8B=E8=AF=95=E5=90=88=E5=B9=B6.?= =?UTF-8?q?txt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 赵峰 <9138985+zhao-feng11@user.noreply.gitee.com> --- "\346\265\213\350\257\225\345\220\210\345\271\266.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\346\265\213\350\257\225\345\220\210\345\271\266.txt" diff --git "a/\346\265\213\350\257\225\345\220\210\345\271\266.txt" "b/\346\265\213\350\257\225\345\220\210\345\271\266.txt" new file mode 100644 index 0000000..d340ec3 --- /dev/null +++ "b/\346\265\213\350\257\225\345\220\210\345\271\266.txt" @@ -0,0 +1 @@ +sda \ No newline at end of file -- Gitee