classComputer{privateStringbrand;privateStringmodel;privatefloatprice;privateintnumber=0;privatebooleanflag=true;privatebooleanflagProToCon=true;publicbooleanisFlagProToC...
class Computer {
private String brand;
private String model;
private float price;
private int number = 0;
private boolean flag = true;
private boolean flagProToCon = true;
public boolean isFlagProToCon() {
return flagProToCon;
}
public void setFlagProToCon(boolean flagProToCon) {
this.flagProToCon = flagProToCon;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void stateChange(){
this.flag = !this.flag;
}
}
class Consumer implements Runnable{
private Computer com = new Computer();
public Consumer(Computer com){
this.com = com;
}
public void run(){
while(this.com.isFlag()){
synchronized(this.com){
if(this.com.isFlagProToCon()){
try{
this.com.wait();
}catch(Exception e){
e.printStackTrace();
}
}
}
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Brand:"+this.com.getBrand()+" Mode:"+this.com.getModel()+
" Price:"+this.com.getPrice()+" this is the"+this.com.getNumber()+"st");
this.com.setFlagProToCon(!this.com.isFlagProToCon());
this.com.notify();
}
}
}
t提示我画斜杠的地方有问题,不知道什么问题?
Brand:dell Mode:Ins14ZD-3526 Price:4000.0 this is the1st
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread not owner
已找出问题
展开