同步线程混合起来

时间:2015-04-14 01:20:37

标签: java multithreading asynchronous

输出:

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 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'
.........

1 个答案:

答案 0 :(得分:1)

看起来你的代码工作得很好 - 生产了一种食物,然后一步一步地食用了一种食物。

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

如果将打印移至同步方法,或者同步调用和打印,则它也应以锁步方式打印。