/*
   SimpleLockTest.java
   
   Description:
      Simple test program that uses the SimpleLock class

   Author:     Moggen
   Homepage:   http://www.moggen.org/
   License:    Public Domain, use as you like

   Log:
      010416   First version
*/

class SimpleLockTest {

   public static void main(String args[]){
      println("Main starts");
      SimpleLock theLock=new SimpleLock();
      Thread t1=new Thread(new threadclass1(theLock));
      Thread t2=new Thread(new threadclass2(theLock));
      println("Main runs threads");
      try{
         t1.start();
         t2.start();
      } catch(Exception e) {}
      println("Main waits for threads to complete");
      try{
         t1.join();
         t2.join();
      } catch(Exception e) {}
      println("Main ends");
   }
         
   static synchronized void println(String str){
      System.out.println(str);
   }
   
   static class threadclass1 implements Runnable {
      SimpleLock theLock;

      threadclass1(SimpleLock l){
         theLock=l;
      }
      
      public void run(){
         int rc;
         SimpleLockTest.println("           Thread 1 starts");
         SimpleLockTest.println("           Thread 1 waits 3 seconds..");
         try {Thread.sleep(3000);} catch(Exception e) {}
         SimpleLockTest.println("           Thread 1 requests the lock..");
         rc=theLock.requestLock();
         SimpleLockTest.println("           Thread 1 got the lock");
         SimpleLockTest.println("           Thread 1 waits 1 second..");
         try {Thread.sleep(1000);} catch(Exception e) {}
         SimpleLockTest.println("           Thread 1 requests the lock again..");
         rc=theLock.requestLock();
         SimpleLockTest.println("           Thread 1 got the lock");
         SimpleLockTest.println("           Thread 1 waits 3 second..");
         try {Thread.sleep(3000);} catch(Exception e) {}
         SimpleLockTest.println("           Thread 1 releases the lock");
         rc=theLock.releaseLock();
         SimpleLockTest.println("           Thread 1 waits 1 second..");
         try {Thread.sleep(1000);} catch(Exception e) {}
         SimpleLockTest.println("           Thread 1 releases the lock");
         rc=theLock.releaseLock();
         SimpleLockTest.println("           Thread 1 ends");
      }
   }

   static class threadclass2 implements Runnable {
      SimpleLock theLock;

      threadclass2(SimpleLock l){
         theLock=l;
      }
      
      public void run(){
         int rc;
         SimpleLockTest.println("                     Thread 2 starts");
         SimpleLockTest.println("                     Thread 2 waits 5 seconds..");
         try {Thread.sleep(5000);} catch(Exception e) {}
         SimpleLockTest.println("                     Thread 2 requests the lock..");
         rc=theLock.requestLock();
         SimpleLockTest.println("                     Thread 2 got the lock");
         SimpleLockTest.println("                     Thread 2 waits 2 seconds..");
         try {Thread.sleep(2000);} catch(Exception e) {}
         SimpleLockTest.println("                     Thread 2 releases the lock");
         rc=theLock.releaseLock();
         SimpleLockTest.println("                     Thread 2 ends");
      }
   }
}
