// BROKEN Readers/Writers with concurrent read or exclusive write // // Program adapted from Greg Andrews' textbook website // http://www.cs.arizona.edu/people/greg/mpdbook/programs/ // then BROKEN by stripping out all of the synchronization! // // Compile: javac BadReadWrite.java // Run: java BadReadWrite rounds import java.util.Random; class Database { private Random generator = new Random(); private int data = 0; // the "database" int nr = 0; private void startRead() { nr++; } private void endRead() { nr--; } public void read() { startRead(); System.out.println("read: " + data); endRead(); } public void write() { int temp; temp = data; data = 99999; // to simulate an inconsistent temporary state try { Thread.sleep(generator.nextInt(500)); // wait a bit } catch (java.lang.InterruptedException e) {} data = temp+1; // back to a safe state System.out.println("wrote: " + data); } } class Reader extends Thread { int rounds; Database RW; private Random generator = new Random(); public Reader(int rounds, Database RW) { this.rounds = rounds; this.RW = RW; } public void run() { for (int i = 0; i