第一次见。。试着Eclipse里编译了下,报错是“A class file was not written. The project may be inconsistent, if so try refreshing this project and building it“,搜了下是你类名起的有问题,con是操作系统里的设备名,CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9 都不能用,把你con都改成了cus。
- package com.hyace.test;
- class Shop
- {
- private String name;
- private int num = 1;
- private boolean flag = false;
- public synchronized void pro(String name)
- {
- if(flag)
- try{wait();}catch(Exception e){}
-
- this.name = name+"----"+num++;
- System.out.println(Thread.currentThread().getName()+"生产--"+name);
-
- flag = true;
-
- notifyAll();
- }
- public synchronized void cus()
- {
- if(!flag)
- try{wait();}catch(Exception e){}
-
- System.out.println(Thread.currentThread().getName()+"消费------"+name);
-
- flag = false;
-
- notifyAll();
- }
- }
- class Pro implements Runnable
- {
- private Shop s;
- Pro(Shop s)
- {
- this.s = s;
- }
- public void run()
- {
- while(true)
- s.pro("娃娃");
- }
- }
- class Cus implements Runnable
- {
- private Shop s;
- Cus(Shop s)
- {
- this.s = s;
- }
- public void run()
- {
- while(true)
- s.cus();
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Shop s = new Shop();
- Pro p = new Pro(s);
- Cus c = new Cus(s);
- Thread t1 = new Thread(p);
- Thread t2 = new Thread(c);
- t1.start();
- t2.start();
- }
- }
复制代码 |