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 readerwriter problem. In exercise we solved the first readerwriter problem with semaphores. Here we will use synchronized instead. All modifications that you need to do for task and can be done in the class RWLock.
Task
Compile the code for ReaderWriter that is given in Seminarzip. How can you see that we have an inconsistent state? Try to formulate why we have this problem?
First solve the first readerwriter 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 waitnotify to solve the problem.Add suitable variables to be able to detect when a thread should wait.
How can you identify the starvation problem?
Task
To solve the problem with starving writers in the first readerwriter 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 readerwriter problem.
The motivation for the second readerwriter 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 readerwriter problem to get a starvation free solution. Instead of modifying your synchronization code with additional condition but we will use a totally different approach
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.loggingLevel;
import java.util.loggingLogger;
public class Data
private static final int INITIALVALUE ;
private int myValue INITIALVALUE;
private RWLock readerAndWriterLock;
public Data
readerAndWriterLock new RWLock;
public void readint id
readerAndWriterLock.acquireRead;
int val myValue;
System.out.printlnReader: id got the value: val;
try
Thread.sleep;
catch InterruptedException ex
Logger.getLoggerDataclass.getNamelogLevelSEVERE, null, ex;
System.out.printlnReader: id thinks that the value is: val but the value is: myValue;
readerAndWriterLock.releaseRead;
public void writeint id int val
readerAndWriterLock.acquireWrite;
myValue val;
System.out.printlnWriter: id updated the value to : val;
readerAndWriterLock.releaseWrite;
package readerwriter;
import java.util.loggingLevel;
import java.util.loggingLogger;
public class Reader implements Runnable
private int myId;
private Data myData;
public Readerint id Data data
myId id;
myData data;
@Override
public void run
performRead;
private void performRead
for int i ; i ; i
try
Thread.sleepintMathrandom;
myData.readmyId;
catch InterruptedException ex
Logger.getLoggerReaderclass.getNamelogLevelSEVERE, null, ex;
package readerwriter;
public class ReaderWriter
public static void mainString args
createThreads;
public static void createThreads
Data data new Data;
createReaderThreadsdata;
createWriterThreadsdata;
private static void createReaderThreadsData data
Thread readerThread;
for int i ; i ; i
readerThread new Threadnew Readeri data;
readerThread.start;
private static void createWriterThreadsData data
Thread writerThread;
for int i ; i ; i
writerThread new Threadnew Writeri data;
writerThread.start;
package readerwriter;
import java.util.loggingLevel;
import java.util.loggingLogger;
public class Writer implements Runnable
private int myId;
private Data myData;
public Writerint idData data
myId id;
myData data;
@Override
public void run
performWrite;
private void performWrite
forint i ; i ; i
try
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
