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
}

djchienaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
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
}
0
sudhakar_koundinyaCommented:
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
}
0
djchienaAuthor Commented:
package org.prithvi.util

What is this import for?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

kiranhkCommented:
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
}
0
sudhakar_koundinyaCommented:
This must be your final code




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
    implements ActionListener {
  public ReadWriteButtons(ReaderWriter controller) {
    theController = controller;
    initComponents();
    setSize(200, 70);
    show();
  }

  private void initComponents() {
    setTitle("Readers/Writers demo");
    setLayout(new GridLayout(1, 2));
    Button read = new Button("Read");
    Button write = new Button("Write");
    add(read);
    add(write);
    read.addActionListener(this);
    write.addActionListener(this);

    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
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
>> 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
0
sudhakar_koundinyaCommented:
comment  Thread.sleep in the code

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

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

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


0
djchienaAuthor Commented:
i took that part out and it's still going crazy on me...weird.
0
djchienaAuthor Commented:
I will be stepping out and wont be back until 7pm est. I hope whatever you can muster helps.  Thanks alot!
0
kiranhkCommented:
ok, assuming u r running the program from the folder where urReaderWriter class file is
java -classpath . ReaderWriter
0
kiranhkCommented:
0
djchienaAuthor Commented:
Thanks buddy! i owe you a big one!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.