Skip to content

Commit c8fa76f

Browse files
committed
Allow virtual thread factory to work with ForkJoinPool
ForkJoinWorkerThread extends Thread (platform) and cannot be virtual. Fall back to a platform ManagedForkJoinWorkerThread instead of throwing UnsupportedOperationException. The TCK expects ForkJoinPool to function with virtual factories — the worker threads are platform but the pool still operates correctly. m>
1 parent 628b663 commit c8fa76f

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

container/openejb-core/src/main/java/org/apache/openejb/threads/impl/ManagedThreadFactoryImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ public Thread newThread(final Runnable r) {
7575

7676
@Override
7777
public ForkJoinWorkerThread newThread(final ForkJoinPool pool) {
78-
if (virtual) {
79-
throw new UnsupportedOperationException("Virtual thread factory does not support ForkJoinPool threads");
80-
}
78+
// ForkJoinWorkerThread extends Thread (platform) — cannot be virtual.
79+
// For virtual factories, fall back to a platform ForkJoinWorkerThread.
8180
return new ManagedForkJoinWorkerThread(pool, priority, contextService);
8281
}
8382

container/openejb-core/src/test/java/org/apache/openejb/threads/impl/ManagedThreadFactoryVirtualTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.concurrent.ForkJoinPool;
3030
import java.util.concurrent.TimeUnit;
3131

32+
import static org.junit.Assert.assertEquals;
3233
import static org.junit.Assert.assertFalse;
3334
import static org.junit.Assert.assertNotNull;
3435
import static org.junit.Assert.assertTrue;
@@ -91,14 +92,23 @@ public void virtualThreadExecutesTask() {
9192
}
9293
}
9394

94-
@Test(expected = UnsupportedOperationException.class)
95-
public void virtualFactoryRejectsForkJoinPool() {
95+
@Test
96+
public void virtualFactoryFallsBackToPlatformForForkJoinPool() {
9697
Assume.assumeTrue("Virtual threads require Java 21+", VirtualThreadHelper.isSupported());
9798

9899
final ContextServiceImpl contextService = ContextServiceImplFactory.newDefaultContextService();
99100
final ManagedThreadFactoryImpl factory = new ManagedThreadFactoryImpl("test-vt-", null, contextService, true);
100101

101-
factory.newThread(new ForkJoinPool());
102+
// ForkJoinWorkerThread cannot be virtual — should fall back to platform thread
103+
final ForkJoinPool pool = new ForkJoinPool(1, factory, null, false);
104+
try {
105+
final java.util.concurrent.Future<String> result = pool.submit(() -> "ok");
106+
assertEquals("ForkJoinPool should work with virtual factory", "ok", result.get(5, java.util.concurrent.TimeUnit.SECONDS));
107+
} catch (final Exception e) {
108+
fail("ForkJoinPool with virtual factory should not throw: " + e);
109+
} finally {
110+
pool.shutdown();
111+
}
102112
}
103113

104114
@Test

0 commit comments

Comments
 (0)