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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李征雪 中级黑马   /  2012-4-6 14:43  /  1360 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李征雪 于 2012-4-6 15:58 编辑
  1. //Demo1205.java

  2. class Commodity//商品类
  3. {
  4.         private String name;
  5.         private int count = 1;
  6.         private boolean flag = false;

  7.         public synchronized void Input(String name)
  8.         {
  9.                 this.name = name +"... ..."+count++;
  10.                 while(true)
  11.                 {
  12.                         while (flag)
  13.                         {       
  14.                                 try
  15.                                 {
  16.                                         wait();
  17.                                 }
  18.                                 catch (Exception e)
  19.                                 {
  20.                                 }
  21.                                 System.out.println(Thread.currentThread().getName()+"生产-->"+name);
  22.                                 flag = !flag;
  23.                                 notifyAll();
  24.                         }
  25.                 }
  26.         }

  27.         public synchronized void Output()
  28.         {
  29.                 while(true)
  30.                 {
  31.                         while (!flag)
  32.                         {       
  33.                                 try
  34.                                 {
  35.                                         wait();
  36.                                 }
  37.                                 catch (Exception e)
  38.                                 {
  39.                                 }
  40.                                 System.out.println(Thread.currentThread().getName()+"消费------>"+name);
  41.                                 flag = !flag;
  42.                                 notifyAll();
  43.                         }
  44.                 }
  45.         }
  46. }

  47. class PersonInput implements Runnable//生产类
  48. {
  49.         Commodity com;
  50.         PersonInput(Commodity com)
  51.         {
  52.                 this.com = com;
  53.         }
  54.         public void run()
  55.         {
  56.                 com.Input("商品");
  57.         }

  58. }

  59. class PersonOutput implements Runnable//消费类
  60. {
  61.         Commodity com;
  62.         PersonOutput(Commodity com)
  63.         {
  64.                 this.com = com;
  65.         }
  66.         public void run()
  67.         {
  68.                 com.Output();       
  69.         }

  70. }

  71. class Demo1205
  72. {
  73.         public static void main(String[] args)
  74.         {
  75.                 Commodity com = new Commodity();

  76.                 PersonInput pi = new PersonInput(com);
  77.                 PersonOutput po = new PersonOutput(com);

  78.                 Thread t1 = new Thread(pi);
  79.                 Thread t2 = new Thread(po);
  80.                 t1.start();
  81.                 t2.start();

  82. //                System.out.println("Hello World!");
  83.         }
  84. }
复制代码
学习毕老师第12天课程写的这段代码,弄了两个小时了,老是一大堆错,现在不报错了,但是没有运行结果,朋友们帮我看看哪里出问题了。

2 个回复

倒序浏览
本帖最后由 黄长利 于 2012-4-6 15:36 编辑

//Demo1205.java

class Commodity//商品类
{
        private String name;
        private int count = 1;
        private boolean flag = false;

        public synchronized void Input(String name)
        {
                this.name = name +"... ..."+count++;
                while(true)
                {
                        while (flag)
                        {        
                                try
                                {
                                      wait();
                                }
                                catch (Exception e)
                                {
                                }
                                
                        }
                        System.out.println(Thread.currentThread().getName()+"生产-->"+name);//此句代码及以下应该写在while(flag)外面,否则,flag初始化为false,只能一直在外循环转悠了
                        flag = !flag;
                        notifyAll();
                }
        }

        public synchronized void Output()
        {
                while(true)
                {
                        while (!flag)
                        {        
                                try
                                {
                                        wait();
                                }
                                catch (Exception e)
                                {
                                }                                
                        }
                        System.out.println(Thread.currentThread().getName()+"消费------>"+name);//此处同上面一样
                        flag = !flag;
                        notifyAll();
                }
        }
}

class PersonInput implements Runnable//生产类
{
        Commodity com;
        PersonInput(Commodity com)
        {
                this.com = com;
        }
        public void run()
        {
                com.Input("商品");
        }

}

class PersonOutput implements Runnable//消费类
{
        Commodity com;
        PersonOutput(Commodity com)
        {
                this.com = com;
        }
        public void run()
        {
                com.Output();        
        }

}

class Demo1205
{
        public static void main(String[] args)
        {
                Commodity com = new Commodity();

                PersonInput pi = new PersonInput(com);
                PersonOutput po = new PersonOutput(com);

                Thread t1 = new Thread(pi);
                Thread t2 = new Thread(po);
                t1.start();
                t2.start();

//                System.out.println("Hello World!");
        }
}
回复 使用道具 举报
谢谢啊,太爽了,鼻子都通气了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马