8291429: java/lang/Thread/virtual/ThreadAPI.java timed out on single core system

Reviewed-by: sspitsyn, cjplummer
This commit is contained in:
Alan Bateman 2022-10-07 08:14:43 +00:00
parent 2d25c0a31c
commit 1fda8421b9
4 changed files with 61 additions and 10 deletions

View File

@ -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);
}
}
/**

View File

@ -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')

View File

@ -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

View File

@ -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;
}
}