changeset 19818:ea208803594d

6815130: Intermittent ThreadMXBean/Locks.java test failure Summary: Preventing stale reads from ThreadExecutionSynchronizer.waiting flag Reviewed-by: dholmes, mchung, dfuchs
author jbachorik
date Fri, 06 Sep 2013 10:03:16 +0200
parents 72338a6a1a4b
children a2cde5f9a3d6
files jdk/test/java/lang/management/ThreadMXBean/Locks.java jdk/test/java/lang/management/ThreadMXBean/ThreadExecutionSynchronizer.java
diffstat 2 files changed, 14 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/test/java/lang/management/ThreadMXBean/Locks.java	Fri Sep 06 00:43:00 2013 -0700
+++ b/jdk/test/java/lang/management/ThreadMXBean/Locks.java	Fri Sep 06 10:03:16 2013 +0200
@@ -193,16 +193,18 @@
         public CheckerThread() {
             super("CheckerThread");
         }
+
+        private void waitForState(Thread.State state) {
+            thrsync.waitForSignal();
+            while (waiter.getState() != state) {
+               goSleep(10);
+            }
+        }
+
         public void run() {
             synchronized (ready) {
                 // wait until WaitingThread about to wait for objC
-                thrsync.waitForSignal();
-
-                int retryCount = 0;
-                while (waiter.getState() != Thread.State.WAITING
-                       && retryCount++ < 500) {
-                   goSleep(100);
-                }
+                waitForState(Thread.State.WAITING);
                 checkBlockedObject(waiter, objC, null, Thread.State.WAITING);
 
                 synchronized (objC) {
@@ -211,16 +213,13 @@
 
                 // wait for waiter thread to about to enter
                 // synchronized object ready.
-                thrsync.waitForSignal();
-                // give chance for waiter thread to get blocked on
-                // object ready.
-                goSleep(50);
+                waitForState(Thread.State.BLOCKED);
                 checkBlockedObject(waiter, ready, this, Thread.State.BLOCKED);
             }
 
             // wait for signal from waiting thread that it is about
             // wait for objC.
-            thrsync.waitForSignal();
+            waitForState(Thread.State.WAITING);
             synchronized(objC) {
                 checkBlockedObject(waiter, objC, Thread.currentThread(), Thread.State.WAITING);
                 objC.notify();
--- a/jdk/test/java/lang/management/ThreadMXBean/ThreadExecutionSynchronizer.java	Fri Sep 06 00:43:00 2013 -0700
+++ b/jdk/test/java/lang/management/ThreadMXBean/ThreadExecutionSynchronizer.java	Fri Sep 06 10:03:16 2013 +0200
@@ -23,7 +23,7 @@
 
 /*
  *
- * @summary Thiseclass is used to synchronize execution off two threads.
+ * @summary This class is used to synchronize execution of two threads.
  * @author  Swamy Venkataramanappa
  */
 
@@ -31,8 +31,8 @@
 
 public class ThreadExecutionSynchronizer {
 
-    private boolean  waiting;
-    private Semaphore semaphore;
+    private volatile boolean waiting;
+    private final Semaphore semaphore;
 
     public ThreadExecutionSynchronizer() {
         semaphore = new Semaphore(1);