|
32 | 32 | import java.lang.reflect.Constructor;
|
33 | 33 | import java.lang.reflect.InvocationTargetException;
|
34 | 34 | import java.lang.reflect.Method;
|
| 35 | +import java.util.concurrent.CountDownLatch; |
35 | 36 | import java.util.concurrent.ExecutorService;
|
36 | 37 | import java.util.concurrent.Executors;
|
37 | 38 | import java.util.concurrent.ThreadFactory;
|
@@ -146,15 +147,19 @@ void testInvokeStatic6() throws Exception {
|
146 | 147 | try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
|
147 | 148 | Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
|
148 | 149 | ThreadFactory factory = builder.factory();
|
| 150 | + |
| 151 | + var ready = new CountDownLatch(1); |
149 | 152 | Thread vthread = factory.newThread(() -> {
|
| 153 | + ready.countDown(); |
150 | 154 | try {
|
151 | 155 | parkMethod.invoke(null); // blocks
|
152 | 156 | } catch (Exception e) { }
|
153 | 157 | });
|
154 | 158 | vthread.start();
|
| 159 | + |
155 | 160 | try {
|
156 |
| - // give thread time to be scheduled |
157 |
| - Thread.sleep(100); |
| 161 | + // wait for thread to run |
| 162 | + ready.await(); |
158 | 163 |
|
159 | 164 | // unpark with another virtual thread, runs on same carrier thread
|
160 | 165 | Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
|
@@ -321,17 +326,27 @@ void testNewInstance6() throws Exception {
|
321 | 326 | try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
|
322 | 327 | Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
|
323 | 328 | ThreadFactory factory = builder.factory();
|
| 329 | + |
| 330 | + var ready = new CountDownLatch(1); |
324 | 331 | Thread vthread = factory.newThread(() -> {
|
| 332 | + ready.countDown(); |
325 | 333 | try {
|
326 | 334 | ctor.newInstance();
|
327 | 335 | } catch (Exception e) { }
|
328 | 336 | });
|
329 | 337 | vthread.start();
|
330 | 338 |
|
331 |
| - Thread.sleep(100); // give thread time to be scheduled |
| 339 | + try { |
| 340 | + // wait for thread to run |
| 341 | + ready.await(); |
332 | 342 |
|
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 | + } |
335 | 350 | }
|
336 | 351 | }
|
337 | 352 |
|
|
0 commit comments