diff --git a/test/jdk/java/lang/Thread/virtual/ThreadAPI.java b/test/jdk/java/lang/Thread/virtual/ThreadAPI.java index 0cf2bd8531b..9476694473f 100644 --- a/test/jdk/java/lang/Thread/virtual/ThreadAPI.java +++ b/test/jdk/java/lang/Thread/virtual/ThreadAPI.java @@ -754,7 +754,7 @@ public class ThreadAPI { /** * Test platform thread invoking timed-Thread.join on a thread that is parking - * and unparking. + * and unparking while pinned. */ @Test public void testJoin33() throws Exception { @@ -775,11 +775,18 @@ public class ThreadAPI { /** * Test virtual thread invoking timed-Thread.join on a thread that is parking - * and unparking. + * and unparking while pinned. */ @Test public void testJoin34() throws Exception { - VThreadRunner.run(this::testJoin33); + // need at least two carrier threads due to pinning + int previousParallelism = VThreadRunner.ensureParallelism(2); + try { + VThreadRunner.run(this::testJoin33); + } finally { + // restore + VThreadRunner.setParallelism(previousParallelism); + } } /** diff --git a/test/jdk/java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java b/test/jdk/java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java index bfaed07494c..52ef8dd4375 100644 --- a/test/jdk/java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java +++ b/test/jdk/java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java @@ -22,22 +22,24 @@ */ /** - * @test + * @test id=default * @bug 8284161 8287103 * @summary Test ThredMXBean.findMonitorDeadlockedThreads with cycles of * platform and virtual threads in deadlock * @enablePreview - * @modules java.management + * @modules java.base/java.lang:+open java.management + * @library /test/lib * @run main/othervm VirtualThreadDeadlocks PP * @run main/othervm VirtualThreadDeadlocks PV * @run main/othervm VirtualThreadDeadlocks VV */ /** - * @test + * @test id=no-vmcontinuations * @requires vm.continuations * @enablePreview - * @modules java.management + * @modules java.base/java.lang:+open java.management + * @library /test/lib * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreadDeadlocks PP * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreadDeadlocks PV * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreadDeadlocks VV @@ -48,6 +50,7 @@ import java.lang.management.ThreadMXBean; import java.util.Arrays; import java.util.concurrent.CyclicBarrier; import java.util.stream.Stream; +import jdk.test.lib.thread.VThreadRunner; public class VirtualThreadDeadlocks { private static final Object LOCK1 = new Object(); @@ -61,6 +64,8 @@ public class VirtualThreadDeadlocks { * VV = test deadlock with two virtual threads */ public static void main(String[] args) throws Exception { + // need at least two carrier threads due to pinning + VThreadRunner.ensureParallelism(2); // start thread1 Thread.Builder builder1 = (args[0].charAt(0) == 'P') diff --git a/test/jdk/java/lang/management/ThreadMXBean/VirtualThreads.java b/test/jdk/java/lang/management/ThreadMXBean/VirtualThreads.java index b986a8cab4f..eff2b62ffae 100644 --- a/test/jdk/java/lang/management/ThreadMXBean/VirtualThreads.java +++ b/test/jdk/java/lang/management/ThreadMXBean/VirtualThreads.java @@ -22,7 +22,7 @@ */ /** - * @test + * @test id=default * @bug 8284161 8290562 * @summary Test java.lang.management.ThreadMXBean with virtual threads * @enablePreview @@ -31,7 +31,7 @@ */ /** - * @test + * @test id=no-vmcontinuations * @requires vm.continuations * @enablePreview * @modules java.base/java.lang:+open java.management diff --git a/test/lib/jdk/test/lib/thread/VThreadRunner.java b/test/lib/jdk/test/lib/thread/VThreadRunner.java index adacfe0b9b5..106739ee7ab 100644 --- a/test/lib/jdk/test/lib/thread/VThreadRunner.java +++ b/test/lib/jdk/test/lib/thread/VThreadRunner.java @@ -23,11 +23,13 @@ package jdk.test.lib.thread; +import java.lang.reflect.Field; import java.time.Duration; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicReference; /** - * Helper class for running tasks in a virtual thread. + * Helper class to support tests running tasks a in virtual thread. */ public class VThreadRunner { private VThreadRunner() { } @@ -140,4 +142,41 @@ public class VThreadRunner { public static void run(ThrowingRunnable task) throws Exception { run(null, 0, task); } + + /** + * Returns the virtual thread scheduler. + */ + private static ForkJoinPool defaultScheduler() { + try { + var clazz = Class.forName("java.lang.VirtualThread"); + var field = clazz.getDeclaredField("DEFAULT_SCHEDULER"); + field.setAccessible(true); + return (ForkJoinPool) field.get(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Sets the virtual thread scheduler's target parallelism. + * @return the previous parallelism level + */ + public static int setParallelism(int size) { + return defaultScheduler().setParallelism(size); + } + + /** + * Ensures that the virtual thread scheduler's target parallelism is at least + * the given size. If the target parallelism is less than the given size then + * it is changed to the given size. + * @return the previous parallelism level + */ + public static int ensureParallelism(int size) { + ForkJoinPool pool = defaultScheduler(); + int parallelism = pool.getParallelism(); + if (size > parallelism) { + pool.setParallelism(size); + } + return parallelism; + } }