生产消费的锁问题,谢谢大家回答!!

public class Test {

public static void main(String[] args) {
    Basket bas = new Basket();
    Thread p = new Thread(new Producer(bas));
    Thread c = new Thread(new Consumer(bas));
    p.start();
    c.start();
}

}

class Basket {
Apple[] arrayApple = new Apple[5];
int index = 0;

public synchronized void push(Apple a) {
    while(index >=5) {
        try {
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    notify();
    arrayApple[index] = a;
    index++;
}
public synchronized Apple pop() {
    while(index <= 0) {
        try {
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    notify();
    index--;
    return arrayApple[index];
}

}
class Apple {
int id;
Apple(int id) {
this.id = id;
}
public String toString() {
return(id + "个苹果");
}
}

class Producer implements Runnable {
Basket bas = new Basket();
Producer(Basket bas) {
this.bas = bas;
}
public void run() {
for(int i=1; i<=10; i++) {
Apple a = new Apple(i);
bas.push(a);
System.out.println("生产了:第" + a);
}
}
}
class Consumer implements Runnable {
Basket bas = new Basket();
Consumer(Basket bas) {
this.bas = bas;
}
public void run() {
for(int i=1; i<=10; i++) {
Apple a = bas.pop();
System.out.println("消费了:第" + a);
}
}
}
打印:
消费了:第1个苹果
生产了:第1个苹果
生产了:第2个苹果
生产了:第3个苹果
消费了:第2个苹果
消费了:第4个苹果
生产了:第4个苹果
消费了:第3个苹果
生产了:第5个苹果
消费了:第5个苹果
消费了:第6个苹果
生产了:第6个苹果
消费了:第7个苹果
生产了:第7个苹果
生产了:第8个苹果
生产了:第9个苹果
消费了:第8个苹果
消费了:第10个苹果
生产了:第10个苹果
消费了:第9个苹果

有时打印如上结果,消费者手太快了,生产苹果在空中他就拿了篮子里的,或者苹果在空中他就接住了,怎么管住他的手?谢谢大家!

我觉得你没有设置等待标志。生产时标示为true,这时不可以消费。生产完成设置成false,这时可以消费但是不能生产,消费完成设置成true。

我自己写的,可以参考,欢迎指正.

 class Info {
    private String name;
    private String title;
    private boolean flag=false;
    int index=1;
    public synchronized void set(String name,String title){
        if(flag){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                }
        this.name=name;
        this.title=title;
        System.out.println("生产第"+(index++)+"个产品");
        flag=true;
        this.notify();
    }
    public synchronized void get(){
        if(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
        System.out.println(this.name+"----"+this.title);
    }
        flag=false;
        this.notify();

}
}
class Producter implements Runnable{
    private Info info;
    public Producter(Info info){
        this.info=info;
    }
    @Override
    public void run() {
        for(int i=0;i<50;i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i%2==0){
                this.info.set("品种1","出口欧美欧美欧美产品");
            }else{
                this.info.set("品种2", "出口东亚产品");
            }
        }

    }
}
class Customer implements Runnable{
    private Info info;
    public Customer(Info info){
        this.info=info;
    }
    @Override
    public void run() {
        for(int i=0;i<50;i++){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print("拿到第"+(i+1)+"个产品,");
            this.info.get();
        }

    }
}

public  class Test {

    public static void main(String[] args) {
        Info info=new Info();
        Runnable r1=new Producter(info);
        Runnable r2=new Customer(info);
        new Thread(r1).start();
        new Thread(r2).start();

    }

}

这种问题用多线程肯定是需要同步的