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 0000000000000000000000000000000000000000..6b76af7a6a4173a71bff8a923e8c12c741e6a69d --- /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(); + } + } + +使用示例: + 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 0000000000000000000000000000000000000000..d340ec3ddcef9b907ede02f64b5d3f694da5d081 --- /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