Question: In this seminar we have three major tasks. The first and second tasks are about the reader - writer problem. In exercise 2 we solved

In this seminar we have three major tasks. The first and second tasks are about the reader-writer problem. In exercise 2 we solved the first reader-writer problem with semaphores. Here we will use synchronized instead. All modifications that you need to do for task 1 and 2, can be done in the class RWLock.
2.1 Task 1
Compile the code for ReaderWriter that is given in Seminar2.zip. How can you see that we have an inconsistent state? Try to formulate why we have this problem?
First solve the first reader-writer problem. In the first reader writer problem a reader should wait only if a writer has locked the data. A writer must wait if readers or writers are active. Use Java synchronized and wait/notify to solve the problem.Add suitable variables to be able to detect when a thread should wait.
How can you identify the starvation problem?
2.2 Task 2
To solve the problem with starving writers in the first reader-writer problem a new condition for
the reader might be added. Here the following additional is added: A new reader are not allowed to start if there are writers waiting for the data. This is defined as the second reader-writer problem.
The motivation for the second reader-writer problem is that it is better to give a writer higher priority because then the readers will read the freshest value.
Add variables to be able to detect waiting writers and modify your code.
Now we will have the problem with reader starvation. Try to identify this in your code.
A solution is to modify the conditions in the reader-writer problem to get a starvation free solution. Instead of modifying your synchronization code with additional condition but we will use a totally different approach
3 Final observations
Before you present your seminar, make sure that you have the answer to the questions given in the
preparations. Also write down the following for each task to discuss during the seminar.
How did you implement the task?
Why did you solve it in the way you did it?
What difference in behaviour did you notice? package readerwriter;
public class RWLock {
public RWLock(){
}
public void acquireRead(){
}
public void acquireWrite(){
}
public void releaseRead(){
}
public void releaseWrite(){
}
} package readerwriter;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Data {
private static final int INITIAL_VALUE =100;
private int myValue = INITIAL_VALUE;
private RWLock readerAndWriterLock;
public Data(){
readerAndWriterLock = new RWLock();
}
public void read(int id){
readerAndWriterLock.acquireRead();
int val = myValue;
System.out.println("Reader:"+ id +" got the value: "+ val);
try {
Thread.sleep(50);
} catch (InterruptedException ex){
Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Reader:"+ id +" thinks that the value is: "+ val +", but the value is: "+ myValue);
readerAndWriterLock.releaseRead();
}
public void write(int id, int val){
readerAndWriterLock.acquireWrite();
myValue = val;
System.out.println("Writer:"+ id +" updated the value to :"+ val);
readerAndWriterLock.releaseWrite();
}
} package readerwriter;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Reader_ implements Runnable {
private int myId;
private Data myData;
public Reader_(int id, Data data){
myId = id;
myData = data;
}
@Override
public void run(){
performRead();
}
private void performRead(){
for (int i =0; i <7; i++){
try {
Thread.sleep((int)(Math.random()*100));
myData.read(myId);
} catch (InterruptedException ex){
Logger.getLogger(Reader_.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} package readerwriter;
public class ReaderWriter {
public static void main(String[] args){
createThreads();
}
public static void createThreads(){
Data data = new Data();
createReaderThreads(data);
createWriterThreads(data);
}
private static void createReaderThreads(Data data){
Thread readerThread;
for (int i =0; i <5; i++){
readerThread = new Thread(new Reader_(i, data));
readerThread.start();
}
}
private static void createWriterThreads(Data data){
Thread writerThread;
for (int i =0; i <2; i++){
writerThread = new Thread(new Writer(i, data));
writerThread.start();
}
}
} package readerwriter;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Writer implements Runnable{
private int myId;
private Data myData;
public Writer(int id,Data data){
myId = id;
myData = data;
}
@Override
public void run(){
performWrite();
}
private void performWrite()
{
for(int i=0 ; i <7; i++){
try {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!