class Info{
private String name="徐彦";    //名称
private String content="程序员"; //内容
private boolean flag=false;   //设置标志位
public synchronized void set(String name,String content){
    if (!flag){
try{
     super.wait();
   }catch(InterruptedException e){
     e.printStackTrace();
   }
    }
this.setName(name);   //设置名称
try{
Thread.sleep(100);       //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
this.setContent(content);  //设置内容
flag=false;
super.notify();
}
public synchronized void get( ){
if (flag){
try{
    super.wait();
   }catch(InterruptedException e){
     e.printStackTrace();
   }
}
try{
 Thread.sleep(100);  //延迟
  }catch(InterruptedException e){
 e.printStackTrace();
  }
  System.out.println(this.getName()+"-->"+this.getContent());
  flag=true;
  super.notify();
}
public void setName(String name){   //设置名称
this.name=name;

public void setContent(String content){  //设置内容
this.content=content;
}
public String getName(){           //取得名称
return this.name;
}
public String getContent(){       //取得内容
return this.content;
}
}
class Producer implements Runnable{   //继承Runnable接口
private Info info=null;    //声明info
      //声明flag
public Producer(Info info){   //通过构造方法设置info
this.info=info;
}
public void run(){   //覆写run()方法
    boolean flag=false; 
for (int i=0;i<50 ;i++ ){
if (flag){
    this.info.set("徐彦","程序员");
flag=false;
}
else{
this.info.set("希哲","教师");
flag=true;
}
}
} }
class Consumer implements Runnable{    //继承Runnable接口
private Info info=null;   //声明info
public Consumer(Info info){
this.info=info;       
}
public void run(){      //覆写run()
for (int i=0;i<50 ;i++ ){
this.info.get();
}
}
}
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info=new Info();      //实例化info
Producer pro=new Producer(info);  //生产者  
Consumer con=new Consumer(info);  //消费者
new Thread(pro).start();
new Thread(con).start();
}
}
   一个生产者与消费者的Demo,小弟有点迷糊,感觉有点明白,但是又不能很清楚,希望哪位大侠现身,解决下啊,小弟在线等啊,谢了 啊 !!!!

解决方案 »

  1.   


    public class ThreadCaseDemo03{ 
    public static void main(String args[]){ 
    Info info=new Info();      //实例化info 
    Producer pro=new Producer(info);  //生产者  
    Consumer con=new Consumer(info);  //消费者 
    new Thread(pro).start(); 
    new Thread(con).start(); 

    先分析这个
    Producer pro=new Producer(info);  //生产者 
    new Thread(pro).start(); 执行它Producer里的run方法
    public void run(){  //覆写run()方法 
        boolean flag=false; 
    for (int i=0;i <50 ;i++ ){ 
    if (flag){ 
        this.info.set("徐彦","程序员"); 
    flag=false; 

    else{ 
    this.info.set("希哲","教师"); 
    flag=true; 



    第一次把this.info.set("希哲","教师");set 进去并且flag=true
      

  2.   


    第一轮flag = flase
    public synchronized void set(String name,String content){ 
        if (!flag){ 
    try{ 
        super.wait(); //线程停止
      }catch(InterruptedException e){ 
        e.printStackTrace(); 
      } 
        } 
    this.setName(name);  //设置名称 
    try{ 
    Thread.sleep(100);      //延迟 
    }catch(InterruptedException e){ 
    e.printStackTrace(); 

    this.setContent(content);  //设置内容 
    flag=false;   
    super.notify(); //唤醒另一个线程消费者 

    Consumer con=new Consumer(info);  //消费者
    new Thread(con).start(); 
    想一想也是开始拿出来打印了其实思路是很简单的。
      

  3.   

      谢谢啊,在下还有两点不解:一开始flag=false;那么if(flag)到底是true还是false?wait()方法让线程等待,那么线程为什么是按一个生产者一个消费者排列的呢?是不是实例化的时候决定的?谢谢大侠啊,谢谢啊!!!
      

  4.   

    一开始flag=false;那么if(flag)到底是true还是false?这个问题我想你应该知道。 你要先清楚flag在整个程序中的流程。那么线程为什么是按一个生产者一个消费者排列的呢?这里有两个关键的方法wait()和notify()你可以check API