Demo entry 6709110

Reader

   

Submitted by anonymous on Feb 01, 2018 at 23:20
Language: Java. Code size: 3.4 kB.

// This file defines class "Reader".

// This code uses
//      class Semaphore, from the java.util.concurrent package in Java 5.0 which defines the behaviour of a 
//                           semaphore, including acquire and release operations.
//      class Synch, which defines the semaphores and variables 
//                   needed for synchronizing the readers and writers.
//      class RandomSleep, which defines the doSleep method.


public class Reader extends Thread {
  int myName;  // The variable myName stores the name of this thread.
               // It is initialized in the constructor for class Reader.

  RandomSleep rSleep;  // rSleep can hold an instance of class RandomSleep.



  // This is the constructor for class Reader.  It has an integer parameter,
  // which is the name that is given to this thread.
  public Reader(int name) {
    myName = name;  // copy the parameter value to local variable "MyName"
    rSleep = new RandomSleep();  // Create an instance of RandomSleep.
  }  // end of the constructor for class "Reader"



  public void run () {
    for (int I = 0;  I < 5; I++) {
      System.out.println("Reader " + myName + " wants to read.  "
                         + " Reader wating is "  + Synch.readwaiting
                         + " Reader active is "  + Synch.readactive);

      // Do acquire on the "mutex" semaphore, to get exclusive access to the
      // variable "Synch.readcount". 
      try{
      	Synch.mutex.acquire();
      }
      catch(Exception e){}
      // If a writer is active, the first reader waits for "wrt" while still 
      // holding on to  "mutex".  Other readers, who arrive later, will get
      //  held up waiting for "mutex". 
      if (Synch.wrtactive > 0 || Synch.wrtwaiting > 0){
    	  Synch.readwaiting ++;
    	  Synch.mutex.release();
    	  
		try{
			 Synch.read.acquire(); 
		}
		catch(Exception e){}
		try{
			 Synch.mutex.acquire(); 
		}
		catch(Exception e){}
   		Synch.readwaiting --;
		
       } // end if
      Synch.readactive ++;
      Synch.mutex.release();
      
      // Now we have permission to start reading.  
      // Print a message and release mutex.
      System.out.println("Reader " + myName + " is now reading.  "
                         + " Reader waiting is " + Synch.readwaiting
                         + " Reader active is "  + Synch.readactive);
      Synch.mutex.release();

      // Simulate the time taken for reading
      rSleep.doSleep(1, 200);   

      // We're finished reading.  Decrement readcount.  If we are the last
      // reader, then signal "wrt".  The signal to "wrt" will either wake up
      // a waiting writer, or set the semaphore value to 1, so that a future
      // writer or reader can go without waiting.
      try{
      	Synch.mutex.acquire();
      }
      catch(Exception e){}
      Synch.readactive --;
      System.out.println("Reader " + myName + " is finished reading.  " 
    		  				+ " Reader waiting is " + Synch.readwaiting
    		  				+ " Reader active is "  + Synch.readactive);
      if (Synch.readactive==0 && Synch.wrtwaiting > 0) Synch.wrt.release();
      Synch.mutex.release();


      // Simulate "doing something else".
      rSleep.doSleep(1, 1000);
    } // end of "for" loop
  }  // end of "run" method
}  // end of class "Reader"

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).