< prev index next > test/jdk/java/lang/Thread/virtual/stress/LotsOfContendedMonitorEnter.java
Print this page
/*
* @test id=default
* @summary Test virtual threads entering a lot of monitors with contention
* @library /test/lib
- * @run main LotsOfContendedMonitorEnter
+ * @run main/timeout=480 LotsOfContendedMonitorEnter
*/
+ import java.time.Instant;
+ import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.CountDownLatch;
import jdk.test.lib.thread.VThreadRunner;
public class LotsOfContendedMonitorEnter {
+ static int depth;
public static void main(String[] args) throws Exception {
- int depth;
if (args.length > 0) {
depth = Integer.parseInt(args[0]);
} else {
depth = 1024;
}
- VThreadRunner.run(() -> testContendedEnter(depth));
+
+ var exRef = new AtomicReference<Throwable>();
+ var thread = Thread.ofVirtual().start(() -> {
+ try {
+ testContendedEnter(depth);
+ } catch (Exception e) {
+ exRef.set(e);
+ }
+ });
+ thread.join();
+ assert exRef.get() == null;
}
/**
* Enter the monitor for a new object, racing with another virtual thread that
* attempts to enter around the same time, then repeat to the given depth.
*/
private static void testContendedEnter(int depthRemaining) throws Exception {
+ boolean doLog = depthRemaining % 10 == 0;
if (depthRemaining > 0) {
var lock = new Object();
// start thread to enter monitor for brief period, then enters again when signalled
var started = new CountDownLatch(1);
// enter, may be contended
synchronized (lock) {
// signal thread to enter monitor again, it should block
signal.countDown();
await(thread, Thread.State.BLOCKED);
+ if (doLog) {
+ System.out.println(Instant.now() + " => at depth: " + (depth - depthRemaining));
+ }
testContendedEnter(depthRemaining - 1);
}
} finally {
thread.join();
}
}
+ if (doLog) {
+ System.out.println(Instant.now() + " => returning from depth: " + (depth - depthRemaining));
+ }
}
/**
* Waits for the given thread to reach a given state.
*/
< prev index next >