黑马程序员技术交流社区

标题: 为什么没有打印啊? [打印本页]

作者: 孙飞    时间: 2012-6-30 12:28
标题: 为什么没有打印啊?
本帖最后由 feigecal 于 2012-6-30 16:05 编辑

class Resource
{
        private String name;
        private int count=1;
        private boolean flag=false;
        public synchronized void setName(String name)
        {
                while(flag)
                {
                                try{this.wait();}catch(Exception e){}
                                this.name=name+"----"+count++;
                                System.out.println(Thread.currentThread().getName()+"---shengchan---"+this.name);
                                flag=true;
                                this.notifyAll();
                }
        }
        public synchronized void out()
        {
                while(!flag)
                        try{this.wait();}catch(Exception e){}
                        System.out.println(Thread.currentThread().getName()+"---xiaofei---"+this.name);
                        flag=false;
                        this.notifyAll();
        }

}
class Producer implements Runnable
{
        private Resource rec;
        Producer(Resource rec)
        {
                this.rec=rec;
        }
        public void run()
        {
                while(true)
                {
                        rec.setName("shangpin");
                }
        }
}
class Consume implements Runnable
{
        private Resource rec;
        Consume(Resource rec)
        {
                this.rec=rec;
        }
        public void run()
        {
                while(true)
                {
                        rec.out();
                }
        }
}
class ShopDemo
{
        public static void main(String[] args)
        {
                Resource r=new Resource();
                //System.out.println("Hello World!");
                Producer p=new Producer(r);
                Consume c=new Consume(r);
                Thread t1=new Thread(p);
                Thread t2=new Thread(p);
                Thread t3=new Thread(c);
                Thread t4=new Thread(c);
                t1.start();
                t2.start();
                t3.start();
                t4.start();
        }
}

作者: 邓超军    时间: 2012-6-30 12:54
    public synchronized void setName(String name)
         {
                 while(flag)
                 {
                                 try{this.wait();}catch(Exception e){}
                                 this.name=name+"----"+count++;
                                 System.out.println(Thread.currentThread().getName()+"---shengchan---"+this.name);
                                 flag=true;
                                 this.notifyAll();
                 }
         }
这段代码错了,这种情况下一旦flag=true,则进程就一直等待,因为线程执行不了try语句块后面的语句,正确的代码是
    public synchronized void setName(String name)
         {
                 while(flag)
                 {
                                 try{this.wait();}catch(Exception e){}
                 }
                                 this.name=name+"----"+count++;
                                 System.out.println(Thread.currentThread().getName()+"---shengchan---"+this.name);
                                 flag=true;
                                 this.notifyAll();
                 
         }
作者: 孙飞    时间: 2012-6-30 16:05
邓超军 发表于 2012-6-30 12:54
public synchronized void setName(String name)
         {
                 while(flag)

就是这个问题




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