publicclassDWTest{publicstaticvoidmain(String[] args){
Car car =newCar();newDrive(car).start();newWashCar(car).start();}}//去洗车classWashCarextendsThread{
Car car;publicWashCar(Car car){this.car = car;}@Overridepublicvoidrun(){for(int i =1; i <30; i++){try{
Thread.sleep(10);}catch(InterruptedException e){
e.printStackTrace();}
car.wash(i);}}}//开车(取车)classDriveextendsThread{
Car car;publicDrive(Car car){this.car = car;}@Overridepublicvoidrun(){//一个月for(int i =1; i <30; i++){try{
Thread.sleep(10);}catch(InterruptedException e){
e.printStackTrace();}
car.drive(i);}}}//车classCar{//初始表示不洗车boolean flag =false;publicsynchronizedvoiddrive(int days){if(flag){try{this.wait();}catch(InterruptedException e){
e.printStackTrace();}}
System.out.println("开车中。。。。。。");
System.out.println("第"+ days +"天该洗车了");this.flag =!this.flag;this.notifyAll();}publicsynchronizedvoidwash(int days){if(!flag){try{this.wait();}catch(InterruptedException e){
e.printStackTrace();}}this.flag =!this.flag;this.notifyAll();
System.out.println("洗好了");}}