Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1904,8 +1904,8 @@ private Object waitingGet(boolean interruptible) {
while ((r = result) == null) {
if (q == null) {
q = new Signaller(interruptible, 0L, 0L);
if (Thread.currentThread() instanceof ForkJoinWorkerThread)
ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q);
if (Thread.currentThread() instanceof ForkJoinWorkerThread wt)
ForkJoinPool.helpAsyncBlocker(wt.pool, q);
}
else if (!queued)
queued = tryPushStack(q);
Expand Down Expand Up @@ -1950,8 +1950,8 @@ else if (nanos <= 0L)
break;
else if (q == null) {
q = new Signaller(true, nanos, deadline);
if (Thread.currentThread() instanceof ForkJoinWorkerThread)
ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q);
if (Thread.currentThread() instanceof ForkJoinWorkerThread wt)
ForkJoinPool.helpAsyncBlocker(wt.pool, q);
}
else if (!queued)
queued = tryPushStack(q);
Expand Down
43 changes: 43 additions & 0 deletions test/jdk/java/util/concurrent/tck/CompletableFutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -5133,4 +5134,46 @@ public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
r.assertInvoked();
}}

public void testOnlyHelpsIfInTheSamePool() throws Exception {
class Logic {
interface Extractor { ForkJoinPool pool(CompletableFuture<ForkJoinPool> cf) throws Exception; }
static final List<ForkJoinPool> executeInnerOuter(
ForkJoinPool outer, ForkJoinPool inner, Logic.Extractor extractor
) throws Exception {
return CompletableFuture.supplyAsync(() ->
Stream.iterate(1, i -> i + 1)
.limit(64)
.map(i -> CompletableFuture.supplyAsync(
() -> Thread.currentThread() instanceof ForkJoinWorkerThread wt ? wt.getPool() : null, inner)
)
.map(cf -> {
try {
return extractor.pool(cf);
} catch (Exception ex) {
throw new AssertionError("Unexpected", ex);
}
})
.toList()
, outer).join();
}
}

List<Logic.Extractor> extractors =
List.of(
c -> c.get(60, SECONDS),
CompletableFuture::get,
CompletableFuture::join
);

try (var pool = new ForkJoinPool(2)) {
for (var extractor : extractors) {
for (var p : Logic.executeInnerOuter(pool, ForkJoinPool.commonPool(), extractor))
assertTrue(p != pool); // The inners should have all been executed by commonPool

for (var p : Logic.executeInnerOuter(pool, pool, extractor))
assertTrue(p == pool); // The inners could have been helped by the outer
}
}
}
}