From 95976ee1a0bd12b35ce61f7978dc9db2d55b92d5 Mon Sep 17 00:00:00 2001 From: dcubed Date: Mon, 24 Mar 2008 14:39:30 -0700 Subject: [PATCH] 4926961: 4/4 TransformerManagementThreadRemoveTests hangs Summary: Changes motivated by Effective Java - Item 48 & Item 51. Reviewed-by: sspitsyn --- .../TransformerManagementThreadAddTests.java | 37 ++++++++++++++++--- ...ransformerManagementThreadRemoveTests.java | 12 ++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/test/java/lang/instrument/TransformerManagementThreadAddTests.java b/test/java/lang/instrument/TransformerManagementThreadAddTests.java index 5c19a4177..de1573a53 100644 --- a/test/java/lang/instrument/TransformerManagementThreadAddTests.java +++ b/test/java/lang/instrument/TransformerManagementThreadAddTests.java @@ -137,9 +137,16 @@ public class TransformerManagementThreadAddTests extends ATestCaseScaffold 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 @@ public class TransformerManagementThreadAddTests extends ATestCaseScaffold 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 @@ public class TransformerManagementThreadAddTests extends ATestCaseScaffold 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 @@ public class TransformerManagementThreadAddTests extends ATestCaseScaffold { 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 @@ public class TransformerManagementThreadAddTests extends ATestCaseScaffold // 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(); } } diff --git a/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java b/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java index 2836bde56..a7d381ff8 100644 --- a/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java +++ b/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java @@ -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 @@ public class TransformerManagementThreadRemoveTests 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()); -- GitLab