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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许飞翔 中级黑马   /  2012-3-27 08:06  /  2276 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class ProducerConsumer
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Res r = new Res();
  6.                 Pro p = new Pro(r);
  7.                 Con c = new Con(r);
  8.                 Thread t1 = new Thread(p);
  9.                 Thread t2 = new Thread(c);
  10.                 t1.start();
  11.                 t2.start();
  12.         }
  13. }
  14. class Res
  15. {
  16.         private String name;
  17.         private int count = 1;
  18.         private boolean flge = false ;
  19.         public synchronized void set(String name)
  20.         {
  21.                 if(flge)
  22.                         try{wait();}catch(Exception e){}
  23.                 this.name = name;
  24.                 System.out.println(Thread.currentThread().getName()+"__生产者__"+this.count);
  25.                 flge = true;
  26.                 this.notify();
  27.         }
  28.         public synchronized void out()
  29.         {
  30.                 if(!flge)
  31.                         try{wait();}catch(Exception e){}
  32.                 System.out.println(Thread.currentThread().getName()+"__消费者__"+this.count);
  33.                 flge = false;
  34.                 this.notify();
  35.         }

  36. }
  37. class Pro implements Runnable
  38. {
  39.         private Res r;
  40.         Pro(Res r)
  41.         {
  42.                 this.r = r;
  43.         }
  44.         public void run()
  45.         {
  46.                 r.set("商品");
  47.         }

  48. }
  49. class Con implements Runnable
  50. {
  51.         private Res r;
  52.         Con(Res r)
  53.         {
  54.                 this.r = r;
  55.         }
  56.         public void run()
  57.         {
  58.                 r.out();
  59.         }
  60. }
复制代码
以上代码一编译就出现了很多特殊符号,我看了好久都没发现问题,真不知道是怎么回事?

111.jpg (29.44 KB, 下载次数: 40)

111.jpg

5 个回复

倒序浏览
  1. import java.util.concurrent.locks.*;
  2. class  shengchandemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 System.out.println("Hello World!");
  7.                 Res r = new Res();
  8.        
  9.                  new Thread(new XiaoFei(r)).start();
  10.                  new Thread(new XiaoFei(r)).start();
  11.                  new Thread(new ShengChan(r)).start();
  12.                  new Thread(new ShengChan(r)).start();

  13.                

  14.         }
  15. }
  16. class Res
  17. {
  18.         private String name;
  19.         private int Jishu=1;
  20.         boolean flag=false;
  21.         private Lock lock = new ReentrantLock();
  22.         private Condition xiaofei =  lock.newCondition();
  23.         private Condition shengchan = lock.newCondition();
  24.         public  void set(String name) throws InterruptedException
  25.         {
  26.                 lock.lock();
  27.                 try
  28.                 {
  29.                         while  (flag)
  30.                                         shengchan.await();       
  31.                 this.name=name+"- -"+Jishu++;
  32.                 System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
  33.                 flag=true;
  34.                 xiaofei.signal();
  35.                 }
  36.                 finally
  37.                 {
  38.                         lock.unlock();
  39.                 }
  40.         }
  41.         public  void out()throws InterruptedException
  42.         {
  43.                 lock.lock();
  44.                 try
  45.                 {
  46.                         while  (!flag)
  47.                                 xiaofei.await();
  48.                 System.out.println(Thread.currentThread().getName()+"......消费者......."+this.name);
  49.                 flag=false;
  50.                 shengchan.signal();
  51.                 }
  52.                 finally
  53.                 {
  54.                 lock.unlock();
  55.                 }
  56.         }
  57. }
  58. class  ShengChan implements Runnable
  59. {
  60.         private Res r;
  61.         ShengChan(Res r)
  62.         {
  63.                 this.r=r;
  64.         }
  65.         public void run()
  66.         {
  67.                 while(true)
  68.                 {
  69.                         try
  70.                         {
  71.                                 r.set("商品");       
  72.                         }
  73.                         catch (InterruptedException e)
  74.                         {
  75.                         }                       
  76.                 }                       
  77.         }
  78. }
  79. class XiaoFei implements Runnable
  80. {
  81.         private Res r;
  82.         XiaoFei (Res r)
  83.         {
  84.                 this.r=r;
  85.         }
  86.         public  void run()
  87.         {
  88.                 while(true)
  89.                 {
  90.                         try
  91.                         {
  92.                                 r.out();
  93.                         }
  94.                         catch (InterruptedException e)
  95.                         {
  96.                         }
  97.                 }
  98.         }
  99. }
复制代码
这种情况估计是哪个字符java虚拟机识别不了,什么错很难查了,
这个代码你参考下吧!或是你全部重新写一遍,把类名和变量名都换掉。
回复 使用道具 举报
类名: 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都不可以.
回复 使用道具 举报
下面是其他的一些保留字,在Windows下面都不能创建。注重,不许分大小写。Con,con,CON 都不可以!

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

The following reserved device names cannot be used as the name of a file: CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed by an extension (for example, NUL.tx7).
回复 使用道具 举报
原来是保留字,多谢各位。
回复 使用道具 举报
windows下,任何文件名都无法保存成con。不区分大小写。都不行。系统默认保留名。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马