[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/lock ...

Manik Surtani msurtani at jboss.com
Mon Dec 4 06:21:57 EST 2006


  User: msurtani
  Date: 06/12/04 06:21:57

  Modified:    tests/functional/org/jboss/cache/lock 
                        ReentrantWriterPreference2Readers1WriterLockTest.java
  Log:
  testing using java 5's util.concurrent
  
  Revision  Changes    Path
  1.3       +92 -55    JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreference2Readers1WriterLockTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ReentrantWriterPreference2Readers1WriterLockTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreference2Readers1WriterLockTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- ReentrantWriterPreference2Readers1WriterLockTest.java	5 May 2006 12:06:58 -0000	1.2
  +++ ReentrantWriterPreference2Readers1WriterLockTest.java	4 Dec 2006 11:21:57 -0000	1.3
  @@ -6,58 +6,68 @@
    */
   package org.jboss.cache.lock;
   
  -import junit.framework.TestCase;
   import junit.framework.Test;
  +import junit.framework.TestCase;
   import junit.framework.TestSuite;
  -import EDU.oswego.cs.dl.util.concurrent.Sync;
   import org.jboss.cache.misc.TestingUtil;
   
  +import java.util.concurrent.locks.ReentrantReadWriteLock;
  +
   /**
    * Tests ReentrantWriterPreferenceReadWriteLock
  + *
    * @author Bela Ban
  - * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.2 2006/05/05 12:06:58 msurtani Exp $
  + * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.3 2006/12/04 11:21:57 msurtani Exp $
    */
  -public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase {
  +public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase
  +{
      // ReentrantWriterPreferenceReadWriteLock lock;
  -   SimpleReadWriteLock lock;
  -   Sync rl, wl;
  -   Exception thread_ex=null;
  +   ReentrantReadWriteLock lock;
  +   ReentrantReadWriteLock.ReadLock rl;
  +   ReentrantReadWriteLock.WriteLock wl;
  +   Exception thread_ex = null;
   
  -   protected void setUp() throws Exception {
  +   protected void setUp() throws Exception
  +   {
         super.setUp();
         // lock=new ReentrantWriterPreferenceReadWriteLock();
  -      lock=new SimpleReadWriteLock();
  -      rl=lock.readLock();
  -      wl=lock.writeLock();
  -      thread_ex=null;
  +      //lock=new SimpleReadWriteLock();
  +      lock = new ReentrantReadWriteLock();
  +      rl = lock.readLock();
  +      wl = lock.writeLock();
  +      thread_ex = null;
      }
   
  -   protected void tearDown() throws Exception {
  +   protected void tearDown() throws Exception
  +   {
         super.tearDown();
  -      lock=null;
  -      if(thread_ex != null)
  +      lock = null;
  +      if (thread_ex != null)
            throw thread_ex;
      }
   
   
  -
  -   private static void log(String msg) {
  +   private static void log(String msg)
  +   {
         System.out.println(System.currentTimeMillis() + "  " + Thread.currentThread() +
                            " [" + Thread.currentThread().getName() + "]: " + msg);
      }
   
  -    public void test2ReadersAnd1Writer() throws InterruptedException {
  -      Upgrader upgrader=new Upgrader("Upgrader");
  -      Reader reader=new Reader("Reader");
  +   public void test2ReadersAnd1Writer() throws InterruptedException
  +   {
  +      Upgrader upgrader = new Upgrader("Upgrader");
  +      Reader reader = new Reader("Reader");
         upgrader.start();
         reader.start();
   
         TestingUtil.sleepThread(500);
  -      synchronized(upgrader) {
  +      synchronized (upgrader)
  +      {
            upgrader.notify();
         }
         TestingUtil.sleepThread(500);
  -      synchronized(reader) {
  +      synchronized (reader)
  +      {
            reader.notify();
         }
         reader.join();
  @@ -66,98 +76,125 @@
      }
   
   
  +   class Reader extends Thread
  +   {
   
  -   class Reader extends Thread {
  -
  -      public Reader(String name) {
  +      public Reader(String name)
  +      {
            super(name);
         }
   
  -      public void run() {
  -         try {
  +      public void run()
  +      {
  +         try
  +         {
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquiring RL");
  -            rl.acquire();
  +            rl.tryLock();
  +            //rl.acquire();
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquired RL");
  -            synchronized(this) {
  +            synchronized (this)
  +            {
                  this.wait();
               }
               ReentrantWriterPreference2Readers1WriterLockTest.log("releasing RL");
  -            rl.release();
  +            rl.unlock();
  +            //rl.release();
               ReentrantWriterPreference2Readers1WriterLockTest.log("released RL");
            }
  -         catch(InterruptedException e) {
  +         catch (InterruptedException e)
  +         {
               ;
            }
         }
      }
   
   
  -   class Writer extends Thread {
  +   class Writer extends Thread
  +   {
   
  -      public Writer(String name) {
  +      public Writer(String name)
  +      {
            super(name);
         }
   
  -      public void run() {
  -         try {
  +      public void run()
  +      {
  +         try
  +         {
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquiring WL");
  -            wl.acquire();
  +            //wl.acquire();
  +            wl.tryLock();
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquired WL");
  -            synchronized(this) {
  +            synchronized (this)
  +            {
                  this.wait();
               }
               ReentrantWriterPreference2Readers1WriterLockTest.log("releasing WL");
  -            wl.release();
  +            //wl.release();
  +            wl.unlock();
               ReentrantWriterPreference2Readers1WriterLockTest.log("released WL");
            }
  -         catch(InterruptedException e) {
  +         catch (InterruptedException e)
  +         {
               ;
            }
         }
      }
   
   
  -   class Upgrader extends Thread {
  -      boolean upgradeSuccessful=false;
  -      public Upgrader(String name) {
  +   class Upgrader extends Thread
  +   {
  +      boolean upgradeSuccessful = false;
  +
  +      public Upgrader(String name)
  +      {
            super(name);
         }
   
  -      public boolean wasUpgradeSuccessful() {
  +      public boolean wasUpgradeSuccessful()
  +      {
            return upgradeSuccessful;
         }
   
   
  -      public void run() {
  -         try {
  +      public void run()
  +      {
  +         try
  +         {
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquiring RL");
  -            rl.acquire();
  +            //rl.acquire();
  +            rl.tryLock();
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquired RL");
  -            synchronized(this) {
  +            synchronized (this)
  +            {
                  this.wait();
               }
               ReentrantWriterPreference2Readers1WriterLockTest.log("attempting to acquire WL");
               // rl.release();
  -            wl.acquire();
  -            upgradeSuccessful=true;
  +            //wl.acquire();
  +            wl.tryLock();
  +            upgradeSuccessful = true;
               ReentrantWriterPreference2Readers1WriterLockTest.log("acquired WL");
               ReentrantWriterPreference2Readers1WriterLockTest.log("releasing WL/RL");
  -            wl.release();
  +            //wl.release();
  +            rl.unlock();
               ReentrantWriterPreference2Readers1WriterLockTest.log("released WL/RL");
            }
  -         catch(InterruptedException e) {
  +         catch (InterruptedException e)
  +         {
               ;
            }
         }
      }
   
   
  -
  -   public static Test suite() {
  +   public static Test suite()
  +   {
         return new TestSuite(ReentrantWriterPreference2Readers1WriterLockTest.class);
      }
   
  -   public static void main(String[] args) {
  +   public static void main(String[] args)
  +   {
         junit.textui.TestRunner.run(ReentrantWriterPreference2Readers1WriterLockTest.suite());
      }
   
  
  
  



More information about the jboss-cvs-commits mailing list