From 6faf05c6ddb3a0bcf4dce9516b8fca15d25cd80f Mon Sep 17 00:00:00 2001 From: Alan Bateman Date: Wed, 2 Aug 2023 10:40:25 +0000 Subject: [PATCH] 8311989: Test java/lang/Thread/virtual/Reflection.java timed out Reviewed-by: jpai, mchung --- .../java/lang/Thread/virtual/Reflection.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/test/jdk/java/lang/Thread/virtual/Reflection.java b/test/jdk/java/lang/Thread/virtual/Reflection.java index bfca37a5e43..4dd34e6c726 100644 --- a/test/jdk/java/lang/Thread/virtual/Reflection.java +++ b/test/jdk/java/lang/Thread/virtual/Reflection.java @@ -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 + } } }