changeset 131:d3dc2ede62ed

4926961: 4/4 TransformerManagementThreadRemoveTests hangs Summary: Changes motivated by Effective Java - Item 48 & Item 51. Reviewed-by: sspitsyn
author dcubed
date Mon, 24 Mar 2008 14:39:30 -0700
parents 7dd94e5bbec4
children bca8bf23ac59
files test/java/lang/instrument/TransformerManagementThreadAddTests.java test/java/lang/instrument/TransformerManagementThreadRemoveTests.java
diffstat 2 files changed, 40 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/lang/instrument/TransformerManagementThreadAddTests.java	Mon Mar 24 14:31:47 2008 -0700
+++ b/test/java/lang/instrument/TransformerManagementThreadAddTests.java	Mon Mar 24 14:39:30 2008 -0700
@@ -137,9 +137,16 @@
             threads[i].start();
         }
 
-        while (!exec.fDone)
+        // Effective Java - Item 48: Synchronize access to shared mutable data
+        // Don't use a direct field getter.
+        while (!exec.isDone())
         {
-            Thread.currentThread().yield();
+            // Effective Java - Item 51: Don't depend on the thread scheduler
+            // Use sleep() instead of yield().
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException ie) {
+            }
         }
         assertTrue(finalCheck());
 
@@ -175,13 +182,17 @@
         this.fExec = exec;
     }
 
+    // Effective Java - Item 48: Synchronize access to shared mutable data
+    // Document a synchronized setter.
     protected synchronized void
     threadFinished(Thread t)
     {
         fFinished++;
     }
 
-    protected boolean
+    // Effective Java - Item 48: Synchronize access to shared mutable data
+    // Provide synchronized getter.
+    protected synchronized boolean
     threadsDone()
     {
         return fFinished == TOTAL_THREADS;
@@ -194,7 +205,9 @@
     protected boolean
     testCompleted()
     {
-        return getExecThread().fDone;
+        // Effective Java - Item 48: Synchronize access to shared mutable data
+        // Don't use direct field getter.
+        return getExecThread().isDone();
     }
 
     /**
@@ -339,6 +352,18 @@
     {
         private boolean fDone = false;
 
+        // Effective Java - Item 48: Synchronize access to shared mutable data
+        // Provide a synchronized getter.
+        private synchronized boolean isDone() {
+            return fDone;
+        }
+
+        // Effective Java - Item 48: Synchronize access to shared mutable data
+        // Provide a synchronized setter.
+        private synchronized void setIsDone() {
+            fDone = true;
+        }
+
         public void
         run()
         {
@@ -349,7 +374,9 @@
 
             // Do a final check for good measure
             executeTransform();
-            fDone = true;
+            // Effective Java - Item 48: Synchronize access to shared mutable data
+            // Don't use direct field setter.
+            setIsDone();
         }
     }
 
--- a/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java	Mon Mar 24 14:31:47 2008 -0700
+++ b/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java	Mon Mar 24 14:39:30 2008 -0700
@@ -27,10 +27,9 @@
  * @summary multi-thread test to exercise sync and contention for removes to transformer registry
  * @author Gabriel Adauto, Wily Technology
  *
- * @ignore Disabled until race condition which hangs test can be fixed.
  * @run build TransformerManagementThreadRemoveTests
- * @run shell MakeJAR.sh basicAgent
- * @run main/othervm -javaagent:basicAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
+ * @run shell MakeJAR.sh redefineAgent
+ * @run main/othervm -javaagent:redefineAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
  */
 import java.util.*;
 
@@ -87,7 +86,12 @@
 
         while (!testCompleted())
         {
-            Thread.currentThread().yield();
+            // Effective Java - Item 51: Don't depend on the thread scheduler
+            // Use sleep() instead of yield().
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException ie) {
+            }
         }
         assertTrue(finalCheck());