黑马程序员技术交流社区

标题: 多线程死锁(notifyAll())问题 [打印本页]

作者: EDDY_Liang    时间: 2014-6-16 19:09
标题: 多线程死锁(notifyAll())问题


  1. //需求:生产烤鸭,生产者生产一份,消费者消费一份,
  2. //且生产者和消费者是多个。

  3. class Resourse
  4. {
  5.         String name;
  6.         int count=1;
  7.         boolean flag = false;
  8.         synchronized void producer()
  9.         {
  10.                
  11.                                 while(this.flag)
  12.                                         try{this.wait();}catch(InterruptedException e){}
  13.                                 System.out.println(Thread.currentThread().getName()+"生产烤鸭"+".........."+this.count);
  14.                                 this.flag = true;
  15.                                 this.notifyAll();

  16.                        
  17.        
  18.         }
  19.         synchronized void customer()
  20.         {
  21.                
  22.                                 while(!this.flag)
  23.                                         try{this.wait();}catch(InterruptedException e){}
  24.                                 System.out.println(Thread.currentThread().getName()+","+"xiaofei"+"..............................."+this.count);
  25.                                 this.flag = false;
  26.                                 this.count++;
  27.                                 this.notifyAll();
  28.                
  29.                        
  30.         }
  31. }
  32. class ShengChan implements Runnable
  33. {
  34.         Resourse r;
  35.         ShengChan(Resourse r)
  36.         {
  37.                 this.r = r;
  38.         }
  39.         public void run()
  40.         {
  41.                 r.producer();
  42.         }
  43. }
  44. class XiaoFei implements Runnable
  45. {
  46.         Resourse r;
  47.         XiaoFei(Resourse r)
  48.         {
  49.                 this.r = r;
  50.         }
  51.         public void run()
  52.         {
  53.                 r.customer();
  54.         }
  55. }


  56. class Test
  57. {
  58.         public static void main(String[] args)
  59.         {
  60.                 Resourse r = new Resourse();
  61.                 ShengChan a = new ShengChan(r);
  62.                 XiaoFei b = new XiaoFei(r);
  63.                 Thread t1 = new Thread(a);
  64.                 Thread t2 = new Thread(a);
  65.                 Thread t3 = new Thread(b);
  66.                 Thread t4 = new Thread(b);
  67.                 t1.start();
  68.                 t2.start();
  69.                 t3.start();
  70.                 t4.start();


  71.         }
  72. }
复制代码

为什么我这个多线程的问题,用notifyAll();了还是死锁啊,试了N次都是每个线程运行了一下。。求大神解答,谢谢
作者: 月光海    时间: 2014-6-16 19:26
没有死锁好不好,你的程序已经结束了,因为你把每个线程只运行了一次,如果要多次运行,请把资源类里的方法中加上循环
作者: 、海    时间: 2014-6-16 19:59
本帖最后由 、海 于 2014-6-16 20:21 编辑

//需求:生产烤鸭,生产者生产一份,消费者消费一份,
//且生产者和消费者是多个。

class Resourse
{
        String name;
        int count=1;
        boolean flag = false;
        synchronized void producer()
        {

                        

作者: 、海    时间: 2014-6-16 20:13
本帖最后由 、海 于 2014-6-16 20:29 编辑

                                while(true){//此处修改
                                     if(flag)  //此处修改        
                                         try{this.wait();}catch(InterruptedException e){}
                                     System.out.println(Thread.currentThread().getName()+"生产烤鸭"+".........."+this.count);
                                } //此处修改
                                this.flag = true;
                                this.notifyAll();

                        
        
        }
作者: 、海    时间: 2014-6-16 20:15
本帖最后由 、海 于 2014-6-16 20:29 编辑

       synchronized void customer(){
               while(true){//此处修改
                          if(!this.flag)//此处修改
                                        try{this.wait();}catch(InterruptedException e){}
                           System.out.println(Thread.currentThread().getName()+","+"xiaofei"+"..............................."+this.count);
                }//此处修改
                this.flag = false;
                this.count++;
                this.notifyAll();        }
}









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