8287200: Test java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java timed out after JDK-8287103

Reviewed-by: kevinw, cjplummer, alanb
This commit is contained in:
Leonid Mesnik 2022-05-27 05:29:25 +00:00
parent ec97da93c1
commit 176bb23de1

View File

@ -35,13 +35,14 @@
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean; import java.lang.management.ThreadMXBean;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.CyclicBarrier;
import java.util.stream.Stream; import java.util.stream.Stream;
public class VirtualThreadDeadlocks { public class VirtualThreadDeadlocks {
private static final Object LOCK1 = new Object(); private static final Object LOCK1 = new Object();
private static final Object LOCK2 = new Object(); private static final Object LOCK2 = new Object();
private static volatile boolean lock2Held; private static final CyclicBarrier barrier = new CyclicBarrier(2);
/** /**
* PP = test deadlock with two platform threads * PP = test deadlock with two platform threads
@ -56,9 +57,7 @@ public class VirtualThreadDeadlocks {
: Thread.ofVirtual(); : Thread.ofVirtual();
Thread thread1 = builder1.start(() -> { Thread thread1 = builder1.start(() -> {
synchronized (LOCK1) { synchronized (LOCK1) {
while (!lock2Held) { try { barrier.await(); } catch (Exception ie) {}
try { Thread.sleep(10); } catch (Exception e) { }
}
synchronized (LOCK2) { } synchronized (LOCK2) { }
} }
}); });
@ -70,7 +69,7 @@ public class VirtualThreadDeadlocks {
: Thread.ofVirtual(); : Thread.ofVirtual();
Thread thread2 = builder2.start(() -> { Thread thread2 = builder2.start(() -> {
synchronized (LOCK2) { synchronized (LOCK2) {
lock2Held = true; try { barrier.await(); } catch (Exception ie) {}
synchronized (LOCK1) { } synchronized (LOCK1) { }
} }
}); });
@ -98,6 +97,9 @@ public class VirtualThreadDeadlocks {
private static void awaitBlocked(Thread thread) throws InterruptedException { private static void awaitBlocked(Thread thread) throws InterruptedException {
while (thread.getState() != Thread.State.BLOCKED) { while (thread.getState() != Thread.State.BLOCKED) {
Thread.sleep(10); Thread.sleep(10);
if (!thread.isAlive()) {
throw new RuntimeException("Thread " + thread + " is terminated.");
}
} }
} }