黑马程序员技术交流社区

标题: 生产者和消费者的问题 [打印本页]

作者: 腹黑生姜    时间: 2015-6-17 16:00
标题: 生产者和消费者的问题
下面这段代码,我转不过来弯,private boolean flag = false既然已经是假了,set这个函数里的 while(flag)是真还是假?为什么毕老师说判断是假,不需要等待?if语句不是满足条件就执行下面语句吗?为什么要跳过wait()呢?
private boolean flag = false;

        public synchronized  void set(String name)
        {    if(flag)
                  try{wait();}catch(Exception e){}
         this.name = name+"--"+count++;
                 System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
                 flag = true;
                 this.notify();
        }

        public synchronized void out()
        {
          if(!flag)
                  try{wait();}catch(Exception e){}
         System.out.println(Thread.currentThread().getName()+"....消费者.............."+this.name);
                 flag = false;
                 this.notify();
                 
        }

作者: Nemo    时间: 2015-6-17 16:54
。。。。。。。。学习中




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2