小弟自己写的实现生产者和消费者的代码,代码如下:public class Test{
public static void main(String[] args){
Test MyTest=new Test();
Test.producer Myproducer=MyTest.new producer();
Test.consumer Myconsumer=MyTest.new consumer();
Myproducer.start();
Myconsumer.start();
}
private static int a=0;
private static boolean b=false;
class producer extends Thread{
public synchronized void produce(){
while(b){
try{
wait();
}catch(Exception e){
e.printStackTrace();
}
}
Test.a=(int)(Math.random()*256);
System.out.println("生成数据:"+Test.a);
Test.b=true;
notify();
}
public void run(){
while(true){
this.produce();
}
}
}

class consumer extends Thread{
public synchronized void consume(){
while(b){
System.out.println("取出数据:"+Test.a);
Test.b=false;
notify();
try{
wait();
}catch(Exception e){
e.printStackTrace();
}
}
}
public void run(){
while(true){
this.consume();
}
}
}
}运行结果:只能实现一次写数据和读数据,请大家帮忙修改一下代码?
谢谢了