|
58 | 58 | import java.util.concurrent.Executor; |
59 | 59 | import java.util.concurrent.ForkJoinPool; |
60 | 60 | import java.util.concurrent.ForkJoinTask; |
| 61 | +import java.util.concurrent.ForkJoinWorkerThread; |
61 | 62 | import java.util.concurrent.RejectedExecutionException; |
62 | 63 | import java.util.concurrent.TimeoutException; |
63 | 64 | import java.util.concurrent.atomic.AtomicInteger; |
@@ -5133,4 +5134,46 @@ public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() { |
5133 | 5134 | checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
5134 | 5135 | r.assertInvoked(); |
5135 | 5136 | }} |
| 5137 | + |
| 5138 | + public void testOnlyHelpsIfInTheSamePool() throws Exception { |
| 5139 | + class Logic { |
| 5140 | + interface Extractor { ForkJoinPool pool(CompletableFuture<ForkJoinPool> cf) throws Exception; } |
| 5141 | + static final List<ForkJoinPool> executeInnerOuter( |
| 5142 | + ForkJoinPool outer, ForkJoinPool inner, Logic.Extractor extractor |
| 5143 | + ) throws Exception { |
| 5144 | + return CompletableFuture.supplyAsync(() -> |
| 5145 | + Stream.iterate(1, i -> i + 1) |
| 5146 | + .limit(64) |
| 5147 | + .map(i -> CompletableFuture.supplyAsync( |
| 5148 | + () -> Thread.currentThread() instanceof ForkJoinWorkerThread wt ? wt.getPool() : null, inner) |
| 5149 | + ) |
| 5150 | + .map(cf -> { |
| 5151 | + try { |
| 5152 | + return extractor.pool(cf); |
| 5153 | + } catch (Exception ex) { |
| 5154 | + throw new AssertionError("Unexpected", ex); |
| 5155 | + } |
| 5156 | + }) |
| 5157 | + .toList() |
| 5158 | + , outer).join(); |
| 5159 | + } |
| 5160 | + } |
| 5161 | + |
| 5162 | + List<Logic.Extractor> extractors = |
| 5163 | + List.of( |
| 5164 | + c -> c.get(60, SECONDS), |
| 5165 | + CompletableFuture::get, |
| 5166 | + CompletableFuture::join |
| 5167 | + ); |
| 5168 | + |
| 5169 | + try (var pool = new ForkJoinPool(2)) { |
| 5170 | + for (var extractor : extractors) { |
| 5171 | + for (var p : Logic.executeInnerOuter(pool, ForkJoinPool.commonPool(), extractor)) |
| 5172 | + assertTrue(p != pool); // The inners should have all been executed by commonPool |
| 5173 | + |
| 5174 | + for (var p : Logic.executeInnerOuter(pool, pool, extractor)) |
| 5175 | + assertTrue(p == pool); // The inners could have been helped by the outer |
| 5176 | + } |
| 5177 | + } |
| 5178 | + } |
5136 | 5179 | } |
0 commit comments