A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张玉建 中级黑马   /  2012-3-29 12:54  /  1740 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class ProducerConsumerDome
{
        public static void main(String[] args)
        {
                Resouer r = new Resouer();
                //Thread t1 = new Thread (r);
                //Thread t2 = new Thread (r);
                //t1.start();
                //t2.start();
                new Thread(new Producer(r)).start();
                new Thread(new Consumer(r)).start();


        }
}
class Resouer
{
        private String name;
        private int courrt = 4;
        boolean flas = false;
        public synchronized void set(String name)
        {
                if(flas)
                try
                {
                        wait();
                }
                catch (Exception e)
                {
                }
                this.name = name+"...."+courrt++;
                System.out.println(Thread.currentThread().getName()+"...生产者."+this.name);
                flas = true;
                this.notify();
        }
        public synchronized void show()
        {
                if(!flas)
                try
                {
                        wait();
                }
                catch (Exception e)
                {
                }
                System.out.println(Thread.currentThread().getName()+"...消费者....."+this.name);
                flas = false;
                this.notify();
        }

}
class Producer implements Runnable
{
        private Resouer res;
        Producer(Resouer res)
        {
                this.res = res;
        }
        public void run()
        {
                while (true)
                {
                        res.set("..商品..");
                }
               
        }
}
class Consumer implements Runnable
{
        private Resouer res;
        Consumer(Resouer res)
        {
                this.res = res;
        }
        public void run()
        {
                while (true)
                {
                        res.show();
                }
               
        }
}


如果我这样定义boolean   flas  =true; 对应的  if(flas).........    flas = false;     和if(!flas)...........flas= true;
那编译运行的结果为什么是这样!!其中发生了什么变换!求解???

QQ截图20120329123715.jpg (67.47 KB, 下载次数: 32)

咋会这样??

咋会这样??

1 个回复

倒序浏览
你的定义改动了。那么相应的判断也需要改变。
set() 里面的判断要改为if(!flas)
show()里面的判断要改为if(flas)

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马