Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

//NAME : Vaishnavi Gaikwad

//Roll No :- UCE2022018
//READER WRITER

import java.util.concurrent.Semaphore;
class RWA {
static Semaphore mutex = new Semaphore(1); //mutex = 1
static Semaphore wrt = new Semaphore(1); //wrt = 1
static int readCount = 0;
//static variables so that all objects of Reader and writer can access the
same variables
//passing 1 to semaphore implies that only one object can access the variable
at a time
static class Reader implements Runnable { //Reader Thread
//Implements runnable interface as runnable interface is used to
execute concurrent threads
public void run() {
//runnable interface has only one function run which is
implemented here
try {
//Acquire Section
mutex.acquire(); //we have to wait till the time mutex is
not 1
//after it is unlocked the thread enters and decrements it
to 0 to lock it for itself again
//now no other thread can enter
//mutex lock ensures no two thread change read count
variable concurrently
readCount++; //reader added
if(readCount==1) //if its the first reader we have to check
if writer is writing
{
wrt.acquire();//we wait till writer does'nt release
the resource once it does we lock it for reader
}
mutex.release(); //after we are done changing readcount we
release mutes to allow other readers to be added

//Reading section
System.out.println("Thread
"+Thread.currentThread().getName() + " is READING");
//current thread is static method which returns current
executing thread
//getname is a method which returns the name of the thread
it is called by
Thread.sleep(1500);
//sleep method is used to pause a thread for few
milliseconds
System.out.println("Thread
"+Thread.currentThread().getName() + " has FINISHED READING");

//Releasing section
mutex.acquire(); //we again put lock to ensure no other
thread changes readcount variable
readCount--; //our reading is done so no. of readers are
decremented
if(readCount == 0) //if not readers are left
{
wrt.release(); //we also release the resource if any
writer want to access it
}
mutex.release(); //we release lock for changing readcount
variable

} catch (InterruptedException e) {
//this exception is thrown when a thread is waiting
sleeping or otherwise occupied
//and the thread is interrupted before or during the
activity
System.out.println(e.getMessage());
}
}
}

static class Writer implements Runnable { //Writer


Thread
public void run() {
try {
//Acquire Section
wrt.acquire(); //if no reader is reading it acquires the
resource
//Writing Section
System.out.println("Thread
"+Thread.currentThread().getName()+" is WRITING");
//current thread is static method which returns current
executing thread
//getname is a method which returns the name of the thread
it is called by
Thread.sleep(1500);
//sleep method is used to pause a thread for few
milliseconds
System.out.println("Thread
"+Thread.currentThread().getName()+" has FINISHED WRITING");
//Releasing section
wrt.release(); //after it is done writing it releases the
lock
} catch (InterruptedException e) {
//this exception is thrown when a thread is waiting
sleeping or otherwise occupied
//and the thread is interrupted before or during the
activity
System.out.println(e.getMessage());
}
}
}

public static void main(String[] args) throws Exception {


Reader read = new Reader(); //new object of reader
Writer write = new Writer(); //new object of writer
Thread r1 = new Thread(read); //3 new thread objects for 3 different
readers
//Thread(Runnable target) type of constructor allocates a new thread
object
r1.setName("Reader1"); //and we set their names
Thread r2 = new Thread(read);
r2.setName("Reader2");
Thread r3 = new Thread(read);
r3.setName("Reader3");

Thread w1 = new Thread(write); //3 new thread objects for 3 different


writers
//Thread(Runnable target) type of constructor allocates a new thread
object
w1.setName("Writer1"); //and we set their names
Thread w2 = new Thread(write);
w2.setName("Writer2");
Thread w3 = new Thread(write);
w3.setName("Writer3");
//start method causes thread to begin execution the JVM calls runs
method of this thread
w1.start();
r1.start();
w2.start();
r2.start();
r3.start();
w3.start();

}
}

/*Output

Thread Writer1 is WRITING


Thread Writer1 has FINISHED WRITING
Thread Writer2 is WRITING
Thread Writer2 has FINISHED WRITING
Thread Reader1 is READING
Thread Reader3 is READING
Thread Reader2 is READING
Thread Reader3 has FINISHED READING
Thread Reader1 has FINISHED READING
Thread Reader2 has FINISHED READING
Thread Writer3 is WRITING
Thread Writer3 has FINISHED WRITING

*/

You might also like