Skip to content

Commit 6faf05c

Browse files
author
Alan Bateman
committedAug 2, 2023
8311989: Test java/lang/Thread/virtual/Reflection.java timed out
Reviewed-by: jpai, mchung
1 parent 5d1b911 commit 6faf05c

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
 

‎test/jdk/java/lang/Thread/virtual/Reflection.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.lang.reflect.Constructor;
3333
import java.lang.reflect.InvocationTargetException;
3434
import java.lang.reflect.Method;
35+
import java.util.concurrent.CountDownLatch;
3536
import java.util.concurrent.ExecutorService;
3637
import java.util.concurrent.Executors;
3738
import java.util.concurrent.ThreadFactory;
@@ -146,15 +147,19 @@ void testInvokeStatic6() throws Exception {
146147
try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
147148
Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
148149
ThreadFactory factory = builder.factory();
150+
151+
var ready = new CountDownLatch(1);
149152
Thread vthread = factory.newThread(() -> {
153+
ready.countDown();
150154
try {
151155
parkMethod.invoke(null); // blocks
152156
} catch (Exception e) { }
153157
});
154158
vthread.start();
159+
155160
try {
156-
// give thread time to be scheduled
157-
Thread.sleep(100);
161+
// wait for thread to run
162+
ready.await();
158163

159164
// unpark with another virtual thread, runs on same carrier thread
160165
Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
@@ -321,17 +326,27 @@ void testNewInstance6() throws Exception {
321326
try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
322327
Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
323328
ThreadFactory factory = builder.factory();
329+
330+
var ready = new CountDownLatch(1);
324331
Thread vthread = factory.newThread(() -> {
332+
ready.countDown();
325333
try {
326334
ctor.newInstance();
327335
} catch (Exception e) { }
328336
});
329337
vthread.start();
330338

331-
Thread.sleep(100); // give thread time to be scheduled
339+
try {
340+
// wait for thread to run
341+
ready.await();
332342

333-
// unpark with another virtual thread, runs on same carrier thread
334-
factory.newThread(() -> LockSupport.unpark(vthread)).start();
343+
// unpark with another virtual thread, runs on same carrier thread
344+
Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
345+
unparker.start();
346+
unparker.join();
347+
} finally {
348+
LockSupport.unpark(vthread); // in case test fails
349+
}
335350
}
336351
}
337352

0 commit comments

Comments
 (0)