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(ReaderWri ter 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).star t();
return true;
} else if(arg.equals("Write")){
new Writer(theController).star t();
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
}
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(ReaderWri
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).star
return true;
} else if(arg.equals("Write")){
new Writer(theController).star
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
try{
theController.readAccess()
System.out.println("Reader
sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
} catch(InterruptedException
System.err.println("A reader was interrupted. Exiting.");
System.exit(1);
}
System.out.println("Reader
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
try{
theController.writeAccess(
System.out.println("Writer
sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
} catch(InterruptedException
System.err.println("A writer was interrupted. Exiting");
System.exit(1);
}
System.out.println("Writer
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(ReaderWri ter 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.WindowAdapt er() {
public void windowClosing(java.awt.eve nt.WindowE vent evt) {
exitForm(evt);
}
});
pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.Wi ndowEvent 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(ActionEven t 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
}
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(ReaderWri
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.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.Wi
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(ActionEven
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
try {
theController.readAccess()
System.out.println("Reader
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
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
try {
theController.writeAccess(
System.out.println("Writer
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
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
package org.prithvi.util
What is this import for?
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.WindowAdapt er;
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(ReaderWri ter 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).star t();
return true;
} else if(arg.equals("Write")){
new Writer(theController).star t();
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
}
import java.awt.Button;
import java.awt.Event;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapt
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(ReaderWri
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
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).star
return true;
} else if(arg.equals("Write")){
new Writer(theController).star
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
try{
theController.readAccess()
System.out.println("Reader
sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
} catch(InterruptedException
System.err.println("A reader was interrupted. Exiting.");
System.exit(1);
}
System.out.println("Reader
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
try{
theController.writeAccess(
System.out.println("Writer
sleep((long) (minSleep + Math.random() * (maxSleep - minSleep)));
} catch(InterruptedException
System.err.println("A writer was interrupted. Exiting");
System.exit(1);
}
System.out.println("Writer
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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
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
it is not necessary for your requirement
ASKER
I seem to be getting an error when I run the program. I get
exception in thread "main" java.lang.noClassDefFoundE rror: ReaderWriter
Any ideas?
exception in thread "main" java.lang.noClassDefFoundE
Any ideas?
did u comment the package??
package org.prithvi.util
package org.prithvi.util
ASKER
i took that part out and it's still going crazy on me...weird.
ASKER
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
java -classpath . ReaderWriter
ASKER
Thanks buddy! i owe you a big one!
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(ReaderWri
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(ActionEven
{
Button b=(Button)ae.getSource();
String arg=b.getLabel();
if (arg.equals("Read")) {
new Reader(theController).star
}
else if (arg.equals("Write")) {
new Writer(theController).star
}
}
ReaderWriter theController;
}
class Reader
extends Thread {
public Reader(ReaderWriter controller) {
id = count++;
theController = controller;
}
public void run() {
System.out.println("Reader
try {
theController.readAccess()
System.out.println("Reader
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
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
try {
theController.writeAccess(
System.out.println("Writer
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
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
}