Notify does nothing unless another thread is waiting. Your code depends solely on the notification (where it needs the condition variable) and relies on the user working with the manufacturer to work.
, ; , . ( , , sb, .) , , . , .
, .
, , , , . , , . , , , , .
:
import java.io.*;
class producer implements Runnable {
StringBuffer sb;
boolean done = false;
producer() {
sb=new StringBuffer("");
}
public void run () {
synchronized(sb) {
for(int i=0;i<10;i++) {
try {
sb.append(i+" ");
System.out.println("Appending ... ");
} catch (Exception e) {}
}
sb.notify();
done = true;
}
}
}
class consumer implements Runnable {
producer p;
consumer(producer pp) {
this.p=pp;
}
public void run() {
System.out.println("Rreached");
synchronized(p.sb) {
try {
while (!p.done) {
p.sb.wait();
}
} catch (Exception e) {}
System.out.println(p.sb);
}
}
}
public class Communication {
public static void main (String [] args) throws Exception {
producer p= new producer();
consumer c= new consumer(p);
Thread t1= new Thread(p);
Thread t2= new Thread(c);
t2.start();
t1.start();
}
}