[exo-jcr-commits] exo-jcr SVN: r1466 - jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Jan 18 09:53:43 EST 2010


Author: nfilotto
Date: 2010-01-18 09:53:43 -0500 (Mon, 18 Jan 2010)
New Revision: 1466

Modified:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestLoadIndexerWriterWithModes.java
Log:
EXOJCR-402: Allow to make all the thread wait and release them by pressing enter

Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestLoadIndexerWriterWithModes.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestLoadIndexerWriterWithModes.java	2010-01-18 14:40:17 UTC (rev 1465)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestLoadIndexerWriterWithModes.java	2010-01-18 14:53:43 UTC (rev 1466)
@@ -26,6 +26,8 @@
 import java.io.InputStreamReader;
 import java.util.Random;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
@@ -44,15 +46,19 @@
    public static final String STATISTIC = "Statistic";
 
    private volatile boolean stop = false;
+   
+   private AtomicBoolean makeThemWait = new AtomicBoolean();
 
-   private int threadCount = 20;
+   private int threadCount = 10;
 
-   //   private int threadCount = 50;
-
    private final CountDownLatch startSignal = new CountDownLatch(1);
 
    private final CountDownLatch doneSignal = new CountDownLatch(threadCount);
 
+   private final CyclicBarrier barrier = new CyclicBarrier(threadCount);
+   
+   private volatile CountDownLatch goSignal;
+   
    private static final String[] words =
       new String[]{"private", "branch", "final", "string", "logging", "bottle", "property", "node", "repository",
          "exception", "cycle", "value", "index", "meaning", "strange", "words", "hello", "outline", "finest",
@@ -60,42 +66,44 @@
 
    public void testWrite() throws Exception
    {
-      try
+      log.info("Skip (y/n) :");
+      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+      String line = reader.readLine();
+      if (!line.equals("y"))
       {
-         System.out.println("Skip (y/n) :");
-         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
-         String line = reader.readLine();
-         if (!line.equals("y"))
+         log.info("Creating threads...");
+         for (int i = 0; i < threadCount; i++)
          {
-            log.info("Creating threads...");
-            for (int i = 0; i < threadCount; i++)
+            // create new thread and start it
+            new Thread(new WriterTask(i)).start();
+            log.info("Thread#" + i + " created and started.");
+         }
+         startSignal.countDown();
+         while (true)
+         {
+            log.info("Type s to stop and return to make the threads wait or to release them :");
+            line = reader.readLine();
+            if (line.equalsIgnoreCase("s"))
             {
-               // create new thread and start it
-               new Thread(new WriterTask(i)).start();
-               log.info("Thread#" + i + " created and started.");
+               break;
             }
-            startSignal.countDown();
-            // wait 5 minutes
-            try
+            else if (makeThemWait.get())
             {
-               Thread.sleep(60000 * 500);
+               makeThemWait.set(false);
+               goSignal.countDown();
             }
-            catch (InterruptedException e)
+            else
             {
-               log.error(e);
+               goSignal = new CountDownLatch(1);
+               makeThemWait.set(true);               
             }
+         }
 
-            stop = true;
-         }
-         else
-         {
-            log.info("Wait for data");
-         }
+         stop = true;
       }
-      catch (Exception e)
+      else
       {
-         // TODO Auto-generated catch block
-         e.printStackTrace();
+         log.info("Wait for data");
       }
 
       doneSignal.await();
@@ -140,39 +148,50 @@
             startSignal.await();
             while (!stop)
             {
+               long time = System.currentTimeMillis();
+               // get any word
+               int i = random.nextInt(words.length);
+               String word = words[i] + id; // "hello12" if thread#12 is creating it
                try
                {
-
-                  long time = System.currentTimeMillis();
-                  // get any word
-                  int i = random.nextInt(words.length);
-                  String word = words[i] + id; // "hello12" if thread#12 is creating it
                   // update statistic
                   updateStatistic(word);
                   // add actual node
                   createTree().addNode(word);
                   sessionLocal.save();
-                  log.info(Thread.currentThread() + " time : " + (System.currentTimeMillis() - time));
+                  System.out.println(Thread.currentThread() + " time : " + (System.currentTimeMillis() - time));
+               }
+               catch (Exception e1)
+               {
+                 log.error("An error occurs", e1);
+               }
 
-                  //               try
-                  //               {
-                  //                  Thread.sleep(300);
-                  //               }
-                  //               catch (InterruptedException e)
-                  //               {
-                  //               }
+               try
+               {
+                  if (makeThemWait.get())
+                  {
+                     barrier.await();
+                     log.info("The threads are waiting for the go signal");
+                     goSignal.await();
+                  }
+                  else
+                  {                     
+                     Thread.sleep(300);
+                  }
                }
-               catch (Exception e)
+               catch (InterruptedException e)
                {
-                  log.error("An unexpected error happens", e);
                }
             }
          }
-         catch (InterruptedException e)
+         catch (Exception e)
          {
+            log.error("An unexpected error happens", e);
+         }
+         finally
+         {
             doneSignal.countDown();
          }
-
       }
 
       /**



More information about the exo-jcr-commits mailing list