Question: Parallel and Distributed Computing 9. The program listed in Figure 1, is an implementation of the reader/writer problem. The program 4 lines with common mistakes
Parallel and Distributed Computing

9. The program listed in Figure 1, is an implementation of the reader/writer problem. The program 4 lines with common mistakes related to concurrent programming. For each of the mistake list the line containing the bug, explain what is wrong and provide a fix to the problem. [8 points) 1) public final class ReadWrite { 2) private int readers = 0; 3) private boolean writing = false; 4) 5) // One reader wants Read access 6) public synchronized void acquireRead() 7) throws InterruptedException { 8) if (writing) 9) wait(); 10) ++readers; 11) } 12) // One reader is leaving 13) public void releaseRead() { 14) --readers; 15) if (readers == 0) 16) notifyAll(); 17) } 18) // One writer wants Write access 19) public synchronized void acquirewrite() 20) throws InterruptedException { 21) if (readers>0 || writing) 22) wait(); 23) writing = true; 24) } 25) // One writer is leaving 26) public void releasewrite() { 27) writing = false; 28) notifyAll(); 29) } 30)} Figure 1: Reader/Writer Class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
