|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2022 Microsoft Corporation and others. |
| 3 | +* All rights reserved. This program and the accompanying materials |
| 4 | +* are made available under the terms of the Eclipse Public License v1.0 |
| 5 | +* which accompanies this distribution, and is available at |
| 6 | +* http://www.eclipse.org/legal/epl-v10.html |
| 7 | +* |
| 8 | +* Contributors: |
| 9 | +* Microsoft Corporation - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.debug.core; |
| 13 | + |
| 14 | +import static java.util.concurrent.CompletableFuture.allOf; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.CompletionException; |
| 20 | +import java.util.concurrent.Executor; |
| 21 | +import java.util.concurrent.ExecutorService; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.function.Supplier; |
| 24 | + |
| 25 | +public class AsyncJdwpUtils { |
| 26 | + /** |
| 27 | + * Create a the thread pool to process JDWP tasks. |
| 28 | + * JDWP tasks are IO-bounded, so use a relatively large thread pool for JDWP tasks. |
| 29 | + */ |
| 30 | + public static ExecutorService jdwpThreadPool = Executors.newWorkStealingPool(100); |
| 31 | + // public static ExecutorService jdwpThreadPool = Executors.newCachedThreadPool(); |
| 32 | + |
| 33 | + public static CompletableFuture<Void> runAsync(List<Runnable> tasks) { |
| 34 | + return runAsync(jdwpThreadPool, tasks.toArray(new Runnable[0])); |
| 35 | + } |
| 36 | + |
| 37 | + public static CompletableFuture<Void> runAsync(Runnable... tasks) { |
| 38 | + return runAsync(jdwpThreadPool, tasks); |
| 39 | + } |
| 40 | + |
| 41 | + public static CompletableFuture<Void> runAsync(Executor executor, List<Runnable> tasks) { |
| 42 | + return runAsync(executor, tasks.toArray(new Runnable[0])); |
| 43 | + } |
| 44 | + |
| 45 | + public static CompletableFuture<Void> runAsync(Executor executor, Runnable... tasks) { |
| 46 | + List<CompletableFuture<Void>> promises = new ArrayList<>(); |
| 47 | + for (Runnable task : tasks) { |
| 48 | + if (task == null) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + promises.add(CompletableFuture.runAsync(task, executor)); |
| 53 | + } |
| 54 | + |
| 55 | + return CompletableFuture.allOf(promises.toArray(new CompletableFuture[0])); |
| 56 | + } |
| 57 | + |
| 58 | + public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { |
| 59 | + return supplyAsync(jdwpThreadPool, supplier); |
| 60 | + } |
| 61 | + |
| 62 | + public static <U> CompletableFuture<U> supplyAsync(Executor executor, Supplier<U> supplier) { |
| 63 | + return CompletableFuture.supplyAsync(supplier, executor); |
| 64 | + } |
| 65 | + |
| 66 | + public static <U> U await(CompletableFuture<U> future) { |
| 67 | + try { |
| 68 | + return future.join(); |
| 69 | + } catch (CompletionException ex) { |
| 70 | + if (ex.getCause() instanceof RuntimeException) { |
| 71 | + throw (RuntimeException) ex.getCause(); |
| 72 | + } |
| 73 | + |
| 74 | + throw ex; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static <U> List<U> await(CompletableFuture<U>[] futures) { |
| 79 | + List<U> results = new ArrayList<>(); |
| 80 | + try { |
| 81 | + allOf(futures).join(); |
| 82 | + for (CompletableFuture<U> future : futures) { |
| 83 | + results.add(await(future)); |
| 84 | + } |
| 85 | + } catch (CompletionException ex) { |
| 86 | + if (ex.getCause() instanceof RuntimeException) { |
| 87 | + throw (RuntimeException) ex.getCause(); |
| 88 | + } |
| 89 | + |
| 90 | + throw ex; |
| 91 | + } |
| 92 | + |
| 93 | + return results; |
| 94 | + } |
| 95 | + |
| 96 | + public static <U> List<U> await(List<CompletableFuture<U>> futures) { |
| 97 | + return await((CompletableFuture<U>[]) futures.toArray(new CompletableFuture[0])); |
| 98 | + } |
| 99 | + |
| 100 | + public static <U> CompletableFuture<List<U>> all(CompletableFuture<U>... futures) { |
| 101 | + return allOf(futures).thenApply((res) -> { |
| 102 | + List<U> results = new ArrayList<>(); |
| 103 | + for (CompletableFuture<U> future : futures) { |
| 104 | + results.add(future.join()); |
| 105 | + } |
| 106 | + |
| 107 | + return results; |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + public static <U> CompletableFuture<List<U>> all(List<CompletableFuture<U>> futures) { |
| 112 | + return allOf(futures.toArray(new CompletableFuture[0])).thenApply((res) -> { |
| 113 | + List<U> results = new ArrayList<>(); |
| 114 | + for (CompletableFuture<U> future : futures) { |
| 115 | + results.add(future.join()); |
| 116 | + } |
| 117 | + |
| 118 | + return results; |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + public static <U> CompletableFuture<List<U>> flatAll(CompletableFuture<List<U>>... futures) { |
| 123 | + return allOf(futures).thenApply((res) -> { |
| 124 | + List<U> results = new ArrayList<>(); |
| 125 | + for (CompletableFuture<List<U>> future : futures) { |
| 126 | + results.addAll(future.join()); |
| 127 | + } |
| 128 | + |
| 129 | + return results; |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + public static <U> CompletableFuture<List<U>> flatAll(List<CompletableFuture<List<U>>> futures) { |
| 134 | + return allOf(futures.toArray(new CompletableFuture[0])).thenApply((res) -> { |
| 135 | + List<U> results = new ArrayList<>(); |
| 136 | + for (CompletableFuture<List<U>> future : futures) { |
| 137 | + results.addAll(future.join()); |
| 138 | + } |
| 139 | + |
| 140 | + return results; |
| 141 | + }); |
| 142 | + } |
| 143 | +} |
0 commit comments