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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄长利 中级黑马   /  2012-3-30 09:15  /  2112 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class ChanPin
  2. {
  3.         private String name;
  4.         private int count = 1;
  5.         private boolean flag = false;
  6.         public void set(String name)
  7.         {
  8.                 while(true)
  9.                 {
  10.                         synchronized(this)
  11.                         {
  12.                                 while(flag)
  13.                                 {
  14.                                         try
  15.                                         {
  16.                                                 this.wait();
  17.                                         }
  18.                                         catch (Exception e)
  19.                                         {
  20.                                         }
  21.                                 }
  22.                                 this.name = name+"..."+count++;
  23.                                 System.out.println("生产商品"+this.name);
  24.                                 flag = true;
  25.                                 this.notifyAll();
  26.                         }
  27.                 }
  28.         }
  29.         public void out()
  30.         {
  31.                 while(true)
  32.                 {
  33.                         synchronized(this)
  34.                         {
  35.                                 while(!flag)
  36.                                 {
  37.                                         try
  38.                                         {
  39.                                                 this.wait();
  40.                                         }
  41.                                         catch (Exception e)
  42.                                         {
  43.                                         }
  44.                                 }
  45.                                 System.out.println("消费商品..........."+name);
  46.                                 flag = false;
  47.                                 this.notifyAll();
  48.                         }
  49.                 }
  50.         }       
  51. }

  52. class Pro implements Runnable
  53. {
  54.         ChanPin cp;
  55.         Pro(ChanPin cp)
  56.         {
  57.                 this.cp = cp;
  58.         }
  59.         public void run()
  60.         {
  61.                 cp.set("百事可乐");
  62.         }
  63. }

  64. class Con implements Runnable
  65. {
  66.         ChanPin cp;
  67.         Con(ChanPin cp)
  68.         {
  69.                 this.cp = cp;
  70.         }
  71.         public void run()
  72.         {
  73.                 cp.out();
  74.         }
  75. }
  76. class ProducerConsumer2
  77. {
  78.         public static void main(String[] args)
  79.         {
  80.                 ChanPin cp = new ChanPin();
  81.                 Pro pro = new Pro(cp);
  82.                 Con con = new Con(cp);
  83.                 Thread t1 = new Thread(pro);
  84.                 Thread t2 = new Thread(con);
  85.                 t1.start();
  86.                 t2.start();
  87.         }
  88. }
复制代码
以上是我写的生产者与消费者问题的代码,可是一编译,当时我就崩溃了,怎么也找不出问题在哪,求大神指导下啊,为什么会出现这样的编译结果:

2 个回复

倒序浏览
下面是其他的一些保留字,在Windows下面都不能创建。注重,不区分大小写。Con,con,CON 都不可以!

con是操作系统保留的一个设备名字,还有很多设备名都不能拿来用,如下:

不能使用的设备名: CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
前两天有个帖子问过同样的问题~~~
回复 使用道具 举报

提示错误:Type name is not valid. 'Con' is an invalid name on this platform.
类型名称是无效的。 'CON'是一个无效的名字,在这个平台上。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马