多线程中if(!flag)的困惑———生产者与消费者的Demo

xzyr2046 2010-03-03 10:49:17
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,小弟有点迷糊,感觉有点明白,但是又不能很清楚,希望哪位大侠现身,解决下啊,小弟在

线等啊,谢了 啊 !!!!
...全文
254 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzyr2046 2010-03-04
  • 打赏
  • 举报
回复
谢谢啊,在下还有两点不解:一开始flag=false;那么if(flag)到底是true还是false?wait()方法让线

程等待,那么线程为什么是按一个生产者一个消费者排列的呢?是不是实例化的时候决定的?谢谢大侠啊,谢

谢啊!!!
SambaGao 2010-03-04
  • 打赏
  • 举报
回复

第一轮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();
想一想也是开始拿出来打印了。。。。。。其实思路是很简单的。
SambaGao 2010-03-04
  • 打赏
  • 举报
回复

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
xzyr2046 2010-03-04
  • 打赏
  • 举报
回复
哪位大侠好心,帮帮忙啊,谢谢啊
SambaGao 2010-03-04
  • 打赏
  • 举报
回复
一开始flag=false;那么if(flag)到底是true还是false?

这个问题我想你应该知道。 你要先清楚flag在整个程序中的流程。

那么线程为什么是按一个生产者一个消费者排列的呢?

这里有两个关键的方法

wait()和notify()

你可以check API
xzyr2046 2010-03-03
  • 打赏
  • 举报
回复
自己顶一个!!!!!顶顶!!!!!!!!!!
xzyr2046 2010-03-03
  • 打赏
  • 举报
回复
就没有老一点的鸟了吗!!!

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧