hello ,
i have tried to write a program on Producer-Consumer. !!
But this program does not runs correctly.
Can anybody tell whass wrong in this program ??
thanx in advance
Prasad
****************************************************
/ producer -Consumer problem
class Storage {
private int contents;
private boolean available =false;
synchronized public int get() {
while (available = false) {
try {
wait();
} catch (Exception e) {
}
}
available = false;
notify();
return contents ;
}
synchronized public void put(int value) {
while (available = true) {
try {
wait();
} catch (Exception e) {
}
}
available = true;
contents = value;
System.out.println("Value is : " + contents);
notifyAll();
}
} //end of class Storage();
class Producer extends Thread {
Storage S1= new Storage();
Producer(Storage s1) {
S1=s1;
}
public void run() {
for(int i=0;i<=10;i++)
{
System.out.println("Putting : " + i );
S1.put(i);
}
} //end of run();
} //end of class Producer
class Consumer extends Thread {
Storage S1= new Storage();
Consumer(Storage s1) {
S1=s1;
}
public void run() {
try { sleep(1000);
} catch(Exception e) {
}
for( int i=0;i<10;i++) {
System.out.println("Getting : " + S1.get());
}
} //end of run()
} //end of class Consumer
****************************************************


Reply With Quote
