同步线程混淆

Synchronised Threads Mixed Up

输出:

Produced food '20'
Produced food '19'
Eaten food '20'
Eaten food '19'
Eaten food '18'
Produced food '18'
Produced food '17'
Eaten food '17'
Produced food '16'
Eaten food '16'
Produced food '15'
Eaten food '15'
Produced food '14'
Eaten food '14'
Produced food '13'
Eaten food '13'
Produced food '12'
Eaten food '12'
Eaten food '11'
Produced food '11'
Produced food '10'
Eaten food '10'
Produced food '9'
Eaten food '9'
Produced food '8'
Eaten food '8'
Produced food '7'
Eaten food '7'
Produced food '6'
Eaten food '6'
Eaten food '5'
Produced food '5'
Produced food '4'
Eaten food '4'
Produced food '3'
Eaten food '3'
Produced food '2'
Eaten food '2'
Produced food '1'
Eaten food '1'
Produced food '0'
Eaten food '0'

我想要一种食物生产然后一种食物按顺序食用

代码:

int times =0;
// read `times` using input

while(times < 20 || times > 100) {
    if (times < 3000) {
        System.out.println("No less than 3000! I am hungry");
        times = (int) cin("----->How much i should eat? in numbers?");
    }
    if(times > 20000){
        System.out.println("I cant eat that much!");
        times = (int) cin("----->How much i should eat? in numbers?");
    }
}
food me = new food();
me.eat(times);

Class代码:

class food{

    public int times;
    public int food;
    public boolean canget=false;

    void eat(int times){

        this.times = times;
        producer p = new producer();
        p.produce(this);
        consumer pe = new consumer();
        pe.consume(this);


    }

    synchronized void add(int n){
        while(canget) {
            try {
                wait();
            } catch (Exception e) {
                System.out.println("Something nasty happened");
            }
        }

        this.food = n;
        canget = true;
        notify();
    }
    synchronized int get(){
        while (!canget){
            try{
                wait();
            }catch (Exception e) {
                System.out.println("Something Nasty happened");
            }
        }
        canget = false;
        notify();
        return this.food;
    }
}

class producer implements Runnable{
    int times;
    food f;

    void produce(food F){
        times=F.times;
        f=F;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        while(this.times-- > 0){
            f.add(times);
            System.out.println("Produced food '"+times+"'");
        }
    }
}

class consumer implements Runnable{

    int times;
    food f;

    void consume(food F){
        times=F.times;
        f=F;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        while(this.times-- > 0){
            f.get();
            System.out.println("Eaten food '"+times+"'");
        }
    }
}

示例输出:

Produced food '20'
Consumed food '20'
Produced food '19'
Consumed food '19'
.........

看起来您的代码运行良好——生产一种食物,然后以同步方式消耗一种食物。

但是,当你写出通知用户的消息时,你是在同步方法之外写的,所以即使没有吃东西,消息也是混乱的。

如果将打印移动到同步方法,或者同步调用和打印,它也应该同步打印。