Link to home
Start Free TrialLog in
Avatar of djchiena
djchiena

asked on

Classical Readers and Writers Problem

Hello all,

I am trying to figure out the Readers/Writers program I made in JAVA.  The problem is, the code I have is deprecated because I am using a very old java book and I want it updated.  Also, I would like to just use one writer instead of multiple writers just to demonstrate.  Can someone help me out in doing this?!  I am rewarding someone 500 points if they can help me out realy fast!!!

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class ReaderWriter{
  public static void main(String[] args){
    new ReadWriteButtons(new ReaderWriter());
  }

  public synchronized void readAccess() throws InterruptedException{
    while(numWriters > 0)
      wait();
    numReaders++;
  }

  public synchronized void writeAccess() throws InterruptedException{
    if (numWriters > 0)
      wait();
    numWriters++;
  }

  public synchronized void doneReading(){
    if(--numReaders == 0)
      notifyAll();
  }

  public synchronized void doneWriting(){
    if(--numWriters == 0)
      notifyAll();
  }

  int numReaders = 0;
  int numWriters = 0;
}

class ReadWriteButtons extends Frame{
  public ReadWriteButtons(ReaderWriter controller){
    theController = controller;
    setTitle("Readers/Writers demo");
    setLayout(new GridLayout(1, 2));
    add(new Button("Read"));
    add(new Button("Write"));
    resize(200, 70);
    show();
  }

  public boolean processEvent(Event evt)
  {
    if (evt.id == Event.WINDOW_DESTROY)
      System.exit(0);
    return super.processEvent(evt);
  }
   
  public boolean action(Event evt, Object arg)
  {
    if(arg.equals("Read")){
      new Reader(theController).start();
      return true;
    } else if(arg.equals("Write")){
      new Writer(theController).start();
      return true;
    } else{
      return super.action(evt, arg);
    }
  }

  ReaderWriter theController;
}

class Reader extends Thread{
  public Reader(ReaderWriter controller){
    id = count++;
    theController = controller;
  }
   
  public void run(){
    System.out.println("Reader number " + id + " arrives");
    try{
      theController.readAccess();
      System.out.println("Reader number " + id + " starts to read");
      sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
    } catch(InterruptedException e){
      System.err.println("A reader was interrupted.  Exiting.");
      System.exit(1);
    }
    System.out.println("Reader number " + id + " finishes reading");
    theController.doneReading();
  }
 
  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}

class Writer extends Thread{
  public Writer(ReaderWriter controller){
    id = count++;
    theController = controller;
  }
   
  public void run(){
    System.out.println("Writer number " + id + " arrives");
    try{
      theController.writeAccess();
      System.out.println("Writer number " + id + " starts to write");
      sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
    } catch(InterruptedException e){
      System.err.println("A writer was interrupted.  Exiting");
      System.exit(1);
    }
    System.out.println("Writer number " + id + " finishes writing");
    theController.doneWriting();
  }
 
  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}

Avatar of sudhakar_koundinya
sudhakar_koundinya

package org.prithvi.util;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class ReaderWriter {
  public static void main(String[] args) {
    new ReadWriteButtons(new ReaderWriter());
  }

  public synchronized void readAccess() throws InterruptedException {
    while (numWriters > 0) {
      wait();
    }
    numReaders++;
  }

  public synchronized void writeAccess() throws InterruptedException {
    if (numWriters > 0) {
      wait();
    }
    numWriters++;
  }

  public synchronized void doneReading() {
    if (--numReaders == 0) {
      notifyAll();
    }
  }

  public synchronized void doneWriting() {
    if (--numWriters == 0) {
      notifyAll();
    }
  }

  int numReaders = 0;
  int numWriters = 0;
}

class ReadWriteButtons
    extends Frame {
  public ReadWriteButtons(ReaderWriter controller) {
    theController = controller;
    setTitle("Readers/Writers demo");
    setLayout(new GridLayout(1, 2));
    add(new Button("Read"));
    add(new Button("Write"));
    setSize(200, 70);
    show();
  }

  public boolean processEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) {
      System.exit(0);
      //  return super.processEvent(evt);
    }
    return false;
  }
 
  public void actionPerformed(ActionEvent ae)
  {
    Button b=(Button)ae.getSource();
    String arg=b.getLabel();
    if (arg.equals("Read")) {
      new Reader(theController).start();
    }
    else if (arg.equals("Write")) {
      new Writer(theController).start();
    }

  }
 

 

  ReaderWriter theController;
}

class Reader
    extends Thread {
  public Reader(ReaderWriter controller) {
    id = count++;
    theController = controller;
  }

  public void run() {
    System.out.println("Reader number " + id + " arrives");
    try {
      theController.readAccess();
      System.out.println("Reader number " + id + " starts to read");
      sleep( (long) (minSleep + Math.random() * (maxSleep - minSleep)));
    }
    catch (InterruptedException e) {
      System.err.println("A reader was interrupted.  Exiting.");
      System.exit(1);
    }
    System.out.println("Reader number " + id + " finishes reading");
    theController.doneReading();
  }

  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}

class Writer
    extends Thread {
  public Writer(ReaderWriter controller) {
    id = count++;
    theController = controller;
  }

  public void run() {
    System.out.println("Writer number " + id + " arrives");
    try {
      theController.writeAccess();
      System.out.println("Writer number " + id + " starts to write");
      sleep( (long) (minSleep + Math.random() * (maxSleep - minSleep)));
    }
    catch (InterruptedException e) {
      System.err.println("A writer was interrupted.  Exiting");
      System.exit(1);
    }
    System.out.println("Writer number " + id + " finishes writing");
    theController.doneWriting();
  }

  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}
This code follows singe Reader and Writer

package org.prithvi.util;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class ReaderWriter {
  public static void main(String[] args) {
    new ReadWriteButtons(new ReaderWriter());
  }

  public synchronized void readAccess() throws InterruptedException {
    while (numWriters > 0) {
      wait();
    }
    numReaders++;
  }

  public synchronized void writeAccess() throws InterruptedException {
    if (numWriters > 0) {
      wait();
    }
    numWriters++;
  }

  public synchronized void doneReading() {
    if (--numReaders == 0) {
      notifyAll();
    }
  }

  public synchronized void doneWriting() {
    if (--numWriters == 0) {
      notifyAll();
    }
  }

  int numReaders = 0;
  int numWriters = 0;
}

class ReadWriteButtons
    extends Frame {
  public ReadWriteButtons(ReaderWriter controller) {
    theController = controller;
    initComponents();
    setSize(200, 70);
    show();
  }

  private void initComponents() {
    setTitle("Readers/Writers demo");
    setLayout(new GridLayout(1, 2));
    add(new Button("Read"));
    add(new Button("Write"));

    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        exitForm(evt);
      }
    });

    pack();
  }

  /** Exit the Application */
  private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
  }

  public boolean processEvent(Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) {
      System.exit(0);
      //  return super.processEvent(evt);
    }
    return false;
  }

  public void actionPerformed(ActionEvent ae) {
    Button b = (Button) ae.getSource();
    String arg = b.getLabel();
    if (arg.equals("Read")) {
      new Reader(theController).run();
    }
    else if (arg.equals("Write")) {
      new Writer(theController).run();
    }

  }

  ReaderWriter theController;
}

class Reader
    {
  public Reader(ReaderWriter controller) {
    id = count++;
    theController = controller;
  }

  public void run() {
    System.out.println("Reader number " + id + " arrives");
    try {
      theController.readAccess();
      System.out.println("Reader number " + id + " starts to read");
      Thread.sleep( (long) (minSleep + Math.random() * (maxSleep - minSleep)));
    }
    catch (InterruptedException e) {
      System.err.println("A reader was interrupted.  Exiting.");
      System.exit(1);
    }
    System.out.println("Reader number " + id + " finishes reading");
    theController.doneReading();
  }

  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}

class Writer
     {
  public Writer(ReaderWriter controller) {
    id = count++;
    theController = controller;
  }

  public void run() {
    System.out.println("Writer number " + id + " arrives");
    try {
      theController.writeAccess();
      System.out.println("Writer number " + id + " starts to write");
      Thread.sleep( (long) (minSleep + Math.random() * (maxSleep - minSleep)));
    }
    catch (InterruptedException e) {
      System.err.println("A writer was interrupted.  Exiting");
      System.exit(1);
    }
    System.out.println("Writer number " + id + " finishes writing");
    theController.doneWriting();
  }

  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}
Avatar of djchiena

ASKER

package org.prithvi.util

What is this import for?
import java.awt.AWTEvent;
import java.awt.Button;
import java.awt.Event;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



public class ReaderWriter{
  public static void main(String[] args){
    new ReadWriteButtons(new ReaderWriter());
  }

  public synchronized void readAccess() throws InterruptedException{
    while(numWriters > 0)
      wait();
    numReaders++;
  }

  public synchronized void writeAccess() throws InterruptedException{
    if (numWriters > 0)
      wait();
    numWriters++;
  }

  public synchronized void doneReading(){
    if(--numReaders == 0)
      notifyAll();
  }

  public synchronized void doneWriting(){
    if(--numWriters == 0)
      notifyAll();
  }

  int numReaders = 0;
  int numWriters = 0;
}

class ReadWriteButtons extends Frame{
  public ReadWriteButtons(ReaderWriter controller){
    theController = controller;
    setTitle("Readers/Writers demo");
    setLayout(new GridLayout(1, 2));
    add(new Button("Read"));
    add(new Button("Write"));
    resize(200, 70);
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            // Exit the application
            System.exit(0);
        }
    });

    show();
   
  }

  public void processEvent(AWTEvent evt)
  {
    if ((int)evt.getID() == Event.WINDOW_DESTROY)
      System.exit(0);
   
        super.processEvent(evt);
  }
   
  public boolean action(Event evt, Object arg)
  {
    if(arg.equals("Read")){
      new Reader(theController).start();
      return true;
    } else if(arg.equals("Write")){
      new Writer(theController).start();
      return true;
    } else{
      return super.action(evt, arg);
    }
  }

  ReaderWriter theController;
}

class Reader extends Thread{
  public Reader(ReaderWriter controller){
    id = count++;
    theController = controller;
  }
   
  public void run(){
    System.out.println("Reader number " + id + " arrives");
    try{
      theController.readAccess();
      System.out.println("Reader number " + id + " starts to read");
      sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
    } catch(InterruptedException e){
      System.err.println("A reader was interrupted.  Exiting.");
      System.exit(1);
    }
    System.out.println("Reader number " + id + " finishes reading");
    theController.doneReading();
  }
 
  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}

class Writer extends Thread{
  public Writer(ReaderWriter controller){
    id = count++;
    theController = controller;
  }
   
  public void run(){
    System.out.println("Writer number " + id + " arrives");
    try{
      theController.writeAccess();
      System.out.println("Writer number " + id + " starts to write");
      sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
    } catch(InterruptedException e){
      System.err.println("A writer was interrupted.  Exiting");
      System.exit(1);
    }
    System.out.println("Writer number " + id + " finishes writing");
    theController.doneWriting();
  }
 
  static int count = 0;
  int id;
  ReaderWriter theController;
  static final int minSleep = 5000; // minimum 1000 ms = 5 second
  static final int maxSleep = 10000; // minimum 5000 ms = 10 seconds
}
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> package org.prithvi.util

nothing special abt this. Genreally I do all tests in this package That's it. the last one is your final code
comment  Thread.sleep in the code

it is not necessary for your requirement
I seem to be getting an error when I run the program.  I get

exception in thread "main" java.lang.noClassDefFoundError: ReaderWriter

Any ideas?
did u comment the package??
 package org.prithvi.util


i took that part out and it's still going crazy on me...weird.
I will be stepping out and wont be back until 7pm est. I hope whatever you can muster helps.  Thanks alot!
ok, assuming u r running the program from the folder where urReaderWriter class file is
java -classpath . ReaderWriter
Thanks buddy! i owe you a big one!