From ff05d9d01cb458e145c4788ae8b979deb77c3421 Mon Sep 17 00:00:00 2001 From: Mythri Alle Date: Tue, 20 Feb 2024 13:48:59 +0000 Subject: Wait for helper thread to finish in MonitorContendedEntered test In testMonitorContendedEnteredForClassMatch, we have main thread and a helper thread contending on the same monitor. We make sure that there is contention by waiting on the main thread till the helper thread is in blocked state before releasing the lock. Though it is possible that we exit since main thread finishes before the helper thread gets a lock. This CL makes the main thread wait for the helper thread to finish. Bug: 142039794 Test: org.apache.harmony.jpda.tests.jdwp.Events_MonitorContendedEnteredTest#testMonitorContendedEnteredForClassMatch Change-Id: I430afe5372c3fea53bfd7c53e4047c57565edfa8 --- .../Events/MonitorContendedEnterAndEnteredDebuggee.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/Events/MonitorContendedEnterAndEnteredDebuggee.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/Events/MonitorContendedEnterAndEnteredDebuggee.java index de5b397..53708d4 100644 --- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/Events/MonitorContendedEnterAndEnteredDebuggee.java +++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/Events/MonitorContendedEnterAndEnteredDebuggee.java @@ -50,13 +50,27 @@ public class MonitorContendedEnterAndEnteredDebuggee extends SyncDebuggee { Thread.yield(); logWriter.println("main thread: Waiting for second thread to attempt to lock a monitor"); } - + // We think the monitor is contended. synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); // Make sure we're good to finish. synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); logWriter.println("--> main thread: finish test"); } + + // Wait for the blocked thread to join. This makes sure we entered the synchronized section + // in the blocked thread and hence the MonitorContentedEntered message should be sent. If we + // don't wait here, there is a possibility we exit the process before the blocked thread + // gets a chance to enter the monitor lock. + boolean done = false; + while (!done) { + try { + thread.join(); + done = true; + } catch(InterruptedException e) { + System.out.println("Thread interrupted when joining, giving another chance"); + } + } } static class BlockedThread extends Thread { -- cgit v1.2.3