8311989: Test java/lang/Thread/virtual/Reflection.java timed out

Reviewed-by: jpai, mchung
This commit is contained in:
Alan Bateman 2023-08-02 10:40:25 +00:00
parent 5d1b911c92
commit 6faf05c6dd

View File

@ -32,6 +32,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@ -146,15 +147,19 @@ class Reflection {
try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
ThreadFactory factory = builder.factory();
var ready = new CountDownLatch(1);
Thread vthread = factory.newThread(() -> {
ready.countDown();
try {
parkMethod.invoke(null); // blocks
} catch (Exception e) { }
});
vthread.start();
try {
// give thread time to be scheduled
Thread.sleep(100);
// wait for thread to run
ready.await();
// unpark with another virtual thread, runs on same carrier thread
Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
@ -321,17 +326,27 @@ class Reflection {
try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
ThreadFactory factory = builder.factory();
var ready = new CountDownLatch(1);
Thread vthread = factory.newThread(() -> {
ready.countDown();
try {
ctor.newInstance();
} catch (Exception e) { }
});
vthread.start();
Thread.sleep(100); // give thread time to be scheduled
try {
// wait for thread to run
ready.await();
// unpark with another virtual thread, runs on same carrier thread
factory.newThread(() -> LockSupport.unpark(vthread)).start();
// unpark with another virtual thread, runs on same carrier thread
Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
unparker.start();
unparker.join();
} finally {
LockSupport.unpark(vthread); // in case test fails
}
}
}