You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by cw...@apache.org on 2009/10/05 21:18:52 UTC

svn commit: r821974 - /incubator/uima/uima-as/trunk/uimaj-as-activemq/src/test/java/org/apache/uima/ee/test/utils/BaseTestSupport.java

Author: cwiklik
Date: Mon Oct  5 19:18:52 2009
New Revision: 821974

URL: http://svn.apache.org/viewvc?rev=821974&view=rev
Log:
UIMA-1189 Replaced explicit synchronization with a semaphore.

Modified:
    incubator/uima/uima-as/trunk/uimaj-as-activemq/src/test/java/org/apache/uima/ee/test/utils/BaseTestSupport.java

Modified: incubator/uima/uima-as/trunk/uimaj-as-activemq/src/test/java/org/apache/uima/ee/test/utils/BaseTestSupport.java
URL: http://svn.apache.org/viewvc/incubator/uima/uima-as/trunk/uimaj-as-activemq/src/test/java/org/apache/uima/ee/test/utils/BaseTestSupport.java?rev=821974&r1=821973&r2=821974&view=diff
==============================================================================
--- incubator/uima/uima-as/trunk/uimaj-as-activemq/src/test/java/org/apache/uima/ee/test/utils/BaseTestSupport.java (original)
+++ incubator/uima/uima-as/trunk/uimaj-as-activemq/src/test/java/org/apache/uima/ee/test/utils/BaseTestSupport.java Mon Oct  5 19:18:52 2009
@@ -28,6 +28,7 @@
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.jms.Message;
@@ -196,7 +197,7 @@
     return (AsynchAEMessage.Request == messageType && AsynchAEMessage.GetMeta == command);
   }
 
-  protected Thread spinMonitorThread(final AtomicBoolean ctrlMonitor, int howMany,
+  protected Thread spinMonitorThread(final Semaphore ctrlSemaphore, int howMany,
           final int aLatchKind) throws Exception {
     final String name;
 
@@ -236,12 +237,8 @@
           // This is needed
           // so that the CASes are send out when the count down latch
           // is ready.
-          synchronized (ctrlMonitor) {
-            ctrlMonitor.set(true);
-            ctrlMonitor.notifyAll();
-          }
+          ctrlSemaphore.release();
           // Wait until the count down latch = 0
-          // cpcLatch.await();
           switch (aLatchKind) {
             case CPC_LATCH:
               // Initialize latch to open after CPC reply comes in.
@@ -275,14 +272,12 @@
     }
   }
 
-  protected void waitOnMonitor(final AtomicBoolean aMonitor) throws Exception {
+  protected void waitOnMonitor(final Semaphore ctrlSemaphore ) throws Exception {
     // Wait until the count down latch thread is ready
-    synchronized (aMonitor) {
-      while (aMonitor.get() == false) {
-        aMonitor.wait();
-      }
-    }
-
+   try {
+     ctrlSemaphore.acquire();
+   } catch( InterruptedException e) {
+   }
   }
 
   protected void runTestWithMultipleThreads(String serviceDeplyDescriptor, String queueName,
@@ -314,11 +309,11 @@
 
     // Wait until the top level service returns its metadata
     waitUntilInitialized();
-    final AtomicBoolean ctrlMonitor = new AtomicBoolean();
-    t2 = spinMonitorThread(ctrlMonitor, howManyCASesPerRunningThread * howManyRunningThreads,
+    final Semaphore ctrlSemaphore = new Semaphore(1);
+    t2 = spinMonitorThread(ctrlSemaphore, howManyCASesPerRunningThread * howManyRunningThreads,
             PROCESS_LATCH);
     // Wait until the CPC Thread is ready.
-    waitOnMonitor(ctrlMonitor);
+    waitOnMonitor(ctrlSemaphore);
 
     if (failOnTimeout) {
       // Spin a thread and wait for awhile before killing the remote service.
@@ -351,7 +346,7 @@
     }
     // Wait until ALL CASes return from the service
     t2.join();
-    t1 = spinMonitorThread(ctrlMonitor, 1, CPC_LATCH);
+    t1 = spinMonitorThread(ctrlSemaphore, 1, CPC_LATCH);
 
     if (!isStopped && !unexpectedException) {
       System.out.println("runTest: Sending CPC");
@@ -371,10 +366,10 @@
   protected void runCrTest(BaseUIMAAsynchronousEngine_impl aUimaEeEngine, int howMany)
           throws Exception {
     engine = aUimaEeEngine;
-    final AtomicBoolean ctrlMonitor = new AtomicBoolean();
-    spinMonitorThread(ctrlMonitor, howMany, PROCESS_LATCH);
+    final Semaphore ctrlSemaphore = new Semaphore(1);
+    spinMonitorThread(ctrlSemaphore, howMany, PROCESS_LATCH);
     aUimaEeEngine.process();
-    waitOnMonitor(ctrlMonitor);
+    waitOnMonitor(ctrlSemaphore);
   }
 
   protected void runTest(Map appCtx, BaseUIMAAsynchronousEngine_impl aUimaEeEngine,
@@ -433,19 +428,21 @@
     // Wait until the top level service returns its metadata
     waitUntilInitialized();
     if (howMany > 0) {
-      final AtomicBoolean ctrlMonitor = new AtomicBoolean();
+      Semaphore ctrlSemaphore = null;
       // Create a thread that will block until an exception is returned,
       // or 2 threads that wait for 'howMany' CASes and then a CPC reply
       if (aLatchKind == EXCEPTION_LATCH) {
-        t1 = spinMonitorThread(ctrlMonitor, 1, EXCEPTION_LATCH);
+        ctrlSemaphore = new Semaphore(1);
+        t1 = spinMonitorThread(ctrlSemaphore, 1, EXCEPTION_LATCH);
       } else {
-        t1 = spinMonitorThread(ctrlMonitor, 1, CPC_LATCH);
-        t2 = spinMonitorThread(ctrlMonitor, howMany, PROCESS_LATCH);
+        ctrlSemaphore = new Semaphore(2);
+        t1 = spinMonitorThread(ctrlSemaphore, 1, CPC_LATCH);
+        t2 = spinMonitorThread(ctrlSemaphore, howMany, PROCESS_LATCH);
       }
 
       if (!isStopped) {
         // Wait until the monitor thread(s) start.
-        waitOnMonitor(ctrlMonitor);
+        waitOnMonitor(ctrlSemaphore);
 
         long startTime = System.currentTimeMillis();
         if (!isStopped) {
@@ -515,19 +512,21 @@
     // Wait until the top level service returns its metadata
     waitUntilInitialized();
     for (int i = 0; i < howMany; i++) {
-      final AtomicBoolean ctrlMonitor = new AtomicBoolean();
+      Semaphore ctrlSemaphore = null;
       // Create a thread that will block until the CPC reply come back
       // from the top level service
       if (aLatchKind == EXCEPTION_LATCH) {
-        t1 = spinMonitorThread(ctrlMonitor, 1, EXCEPTION_LATCH);
+        ctrlSemaphore = new Semaphore(1);
+        t1 = spinMonitorThread(ctrlSemaphore, 1, EXCEPTION_LATCH);
       } else {
-        t1 = spinMonitorThread(ctrlMonitor, 1, CPC_LATCH);
-        t2 = spinMonitorThread(ctrlMonitor, 1, PROCESS_LATCH);
+        ctrlSemaphore = new Semaphore(2);
+        t1 = spinMonitorThread(ctrlSemaphore, 1, CPC_LATCH);
+        t2 = spinMonitorThread(ctrlSemaphore, 1, PROCESS_LATCH);
       }
 
       if (!isStopped) {
         // Wait until the CPC Thread is ready.
-        waitOnMonitor(ctrlMonitor);
+        waitOnMonitor(ctrlSemaphore);
         if (!isStopped) {
           // Send an in CAS to the top level service
           sendCAS(aUimaEeEngine, 1, sendCasAsynchronously);