0% found this document useful (0 votes)
27 views

Program

The document defines a reader-writer problem solution in Java using threads and synchronization. It creates a lock object and monitor classes for readers and writers. Readers can read shared data concurrently, while writers have exclusive access to write and increment the data. The program creates reader and writer threads, starts them, and outputs the thread IDs and data values as they perform read and write operations in synchronized blocks using the lock object.

Uploaded by

Sreejith Ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Program

The document defines a reader-writer problem solution in Java using threads and synchronization. It creates a lock object and monitor classes for readers and writers. Readers can read shared data concurrently, while writers have exclusive access to write and increment the data. The program creates reader and writer threads, starts them, and outputs the thread IDs and data values as they perform read and write operations in synchronized blocks using the lock object.

Uploaded by

Sreejith Ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM

import java.io.*;
import java.lang.*;
class lock{}
class rwmonitor extends Thread
{
public static lock l=new lock();
public static boolean writing=false;
public int thread_id;
public static int data=0;
public static int reader_num=0;
String type;
rwmonitor(int i,String s)
{
thread_id=i;
type=s;
}
public void run()
{
if(type.equals("reader"))
{
keepreading();
}
else
{
keepwriting();
}
}
public void keepreading()

{
acquirereadlock();
System.out.println(type+" "+thread_id+" reads "+data);
releasedreadlock();
}
public void keepwriting()
{
acquirewritelock();
data++;
System.out.println(type+" "+thread_id+" writes "+data);
releasedwritelock();
}
public void acquirereadlock()
{
synchronized(l)
{
while(writing)
{try{
System.out.println(type+" "+thread_id+"waiting");
l.wait();
}catch(InterruptedException e){}}
reader_num++;
}
}
public void acquirewritelock()
{

synchronized(l)
{

while(writing||reader_num>0)
{try{
System.out.println(type+" "+thread_id+"waiting");
l.wait();
}catch(InterruptedException e){}}
writing=true;
}
}
public void releasedreadlock()
{
synchronized(l)
{
--reader_num;
System.out.println("reader"+thread_id+" complete");
if(reader_num==0)
{
l.notifyAll();
}
}
}
public void releasedwritelock()
{
synchronized(l)
{
writing=false;
System.out.println("writer"+thread_id+" complete");
l.notifyAll();
}
}

}
class rw
{
public static void main(String args[])throws IOException
{
int i;
rwmonitor a[ ]=new rwmonitor[6];
for(i=0;i<3;i++)
a[i]=new rwmonitor(i,"reader");
for(i=3;i<6;i++)
a[i]=new rwmonitor(i,"writer");
for(i=5;i>=0;i--)
a[i].start();
}
}

OUTPUT
proglab34@proglab34:~/sr$ javac rw.java
proglab34@proglab34:~/sr$ java rw
writer 5 writes 1

writer5 complete
writer 4 writes 2
writer4 complete
writer 3 writes 3
reader 2waiting
writer3 complete
reader 2 reads 3
reader2 complete
reader 1 reads 3
reader1 complete
reader 0 reads 3
reader0 complete

You might also like