RT,这是我的程序, 为什么运行结果总是先消费了一个馒头才开始生产消费正常化。。难道是我的WAIT和NOTIFY用的不对嘛?
public class WQ{
public static void main(String [] args){
LanZi lz = new LanZi();
Producer p = new Producer(lz);
Consumer c = new Consumer(lz);
new Thread(p).start();
new Thread(c).start();
}
}class ManTou{
int id;
ManTou(int id){
this.id = id;
}

public String toString(){
return "ManTou" + id;
}
}class LanZi{
ManTou[] arrMT = new ManTou[6];
int index = 0;

public synchronized void jia(ManTou MT){
while(index==arrMT.length){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notify();
arrMT[index] = MT;
index++;
}

public synchronized ManTou jian(){
while(index==0){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notify();
index--;
return arrMT[index];
}
}class Producer implements Runnable{
LanZi lz = null;
Producer(LanZi lz){
this.lz = lz;
}

public void run(){
for(int i=0;i<20;i++){
ManTou mt = new ManTou(i+1);
lz.jia(mt);
System.out.println("生产了"+mt);
/* try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
*/
}
}
}class Consumer implements Runnable{
LanZi lz = null;
Consumer(LanZi lz){
this.lz = lz;
}

public void run(){
for(int i=0;i<20;i++){
ManTou mt = lz.jian();
System.out.println("消费了"+mt);
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}