62,567
社区成员




第一轮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(); //唤醒另一个线程消费者
}
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里的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