Hi,
Page 214 in RHE has the code for Consumer3, but the code in that version of the program isn't intended to call properly formed
thread methods. Below, is my version of Consumer3 calling the methods in Mailbox3.
How am I going to call the synchronized methods in Mailbox3? I can't call them with an instance of Thread because Thread doesn't have those methods. It's no use to make Mailbox3 a Runnable because it doesn't use the run() method. And I can't use the run() method because run() won't accept anything passed into it.
Thanks!
import java.lang.*;
public class Consumer3 extends Thread {
Mailbox3 m3 = new Mailbox3();
Thread t = new Thread(m3);
Thread t2 = new Thread(m3);
public void run() {
t.meth2();
t2.meth1("hey everybody");
}
public static void main(
String args[]) {
Consumer3 c3 = new Consumer3();
c3.start();
}
}
import java.lang.*;
class Mailbox3 implements Runnable {
private boolean request;
private String message;
public void run();
public synchronized void meth1(String message) {
while(request == true) {
try {
wait();
} catch (InterruptedException ie) {
}
}
request = true;
this.message = message;
notify();
}
public synchronized String meth2() {
System.out.println("I'm in meth2()");
while(request == false) {
System.out.println("request = this in meth2() " + request);
try {
wait();
} catch (InterruptedException e) { }
}
request = false;
notify();
return message;
}
}