Merge "Wait for helper thread to finish in MonitorContendedEntered test" into main am: 92b03ef02b

Original change: https://android-review.googlesource.com/c/platform/external/apache-harmony/+/2969271

Change-Id: I8740f7fad3cd61a3e2f007648013735860895283
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
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 @@
                 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 {