等到线程完成

用户名

主类:

producer p = new producer();
food me = new food();
me.eat(times,p);
....>Rest of Code<.......

其他班级:

class food {

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



   void eat(int times,producer p){

       this.times = times;
       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;
        System.out.println("Produced food '"+n+"'");
        canget = true;
        notify();
    }
    synchronized int get(){
        while (!canget){
            try{
                wait();
            }catch (Exception e) {
                System.out.println("Something Nasty happened");
            }
        }
        canget = false;
        notify();
        System.out.println("Eaten food '"+this.food+"'");
        return this.food;
    }

}

class producer implements Runnable{
    int times;
    food f;
    boolean done;

    boolean done(Thread t) {
        return done;
    }

    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);

        }

    }
}

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();
        }
    }
}

在此声明之后:

me.eat(times,p);

代码的其余部分正在运行,但我想,经过线程通过运行foodproducerconsumer完成。我怎样才能做到这一点?

奥利维·克罗斯(Olivier Croisier)

您可以使用CountDownLatch允许生产者和使用者在完成操作时发出信号,并在主代码中等待该信号。

主要代码:

int times = 3;

CountDownLatch completion = new CountDownLatch(2);

producer p = new producer(completion);
food me = new food(completion);
me.eat(times, p);


try {
    completion.await();
} catch (InterruptedException e) {
    e.printStackTrace();
}

System.out.println("Done");

更新的课程:

class food {

    public int times;
    public int food;
    public boolean canget = false;
    private CountDownLatch completion;

    public food(CountDownLatch completion) {
        this.completion = completion;
    }

    void eat(int times, producer p) {
        this.times = times;
        p.produce(this);
        consumer pe = new consumer(completion);
        pe.consume(this);
    }

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

        this.food = n;
        System.out.println("Produced food '" + n + "'");
        canget = true;
        notify();
    }

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

}

class producer implements Runnable {
    int times;
    food f;
    boolean done;
    private CountDownLatch completion;

    public producer(CountDownLatch completion) {
        this.completion = completion;
    }

    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);
        }
        completion.countDown();
    }
}

class consumer implements Runnable {

    int times;
    food f;
    private CountDownLatch completion;

    public consumer(CountDownLatch completion) {
        this.completion = completion;
    }

    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();
        }
        completion.countDown();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

等到我的线程完成

来自分类Dev

Perl如何让线程等到Shell完成命令执行

来自分类Dev

主线程中的多线程矩阵乘法是否不等到其他线程完成其工作?

来自分类Dev

等到函数完成后再继续执行主线程(一个线程)

来自分类Dev

等到forEach循环完成?

来自分类Dev

等到gobalEval完成

来自分类Dev

等到功能完成

来自分类Dev

Android AsyncTask等到完成

来自分类Dev

等到迅速完成scrollToRowAtIndexPath

来自分类Dev

等到语句完成

来自分类Dev

等到gobalEval完成

来自分类Dev

等到进程完成运行

来自分类Dev

程序必须等到两个线程(Sql 查询执行)完成

来自分类Dev

等到线程列表结束

来自分类Dev

AngularJS等到许多$ timeouts完成

来自分类Dev

PhantomJS javascript等到功能完成

来自分类Dev

PhantomJS javascript等到功能完成

来自分类Dev

CSS转换等到转换完成

来自分类Dev

等到fs.readFileSync完成

来自分类Dev

Sequelize等到循环以回调完成

来自分类Dev

Python:如何使程序等到函数或方法完成

来自分类Dev

等到多个命令行过程完成?

来自分类Dev

让JavaFX等到时间轴完成

来自分类Dev

javascript等到DOM元素实际绘制完成

来自分类Dev

等到$ http完成以在AngularJS中输出结果

来自分类Dev

等到AJAX在$ .each循环内完成

来自分类Dev

如何使Gulp等到dest文件完成?

来自分类Dev

等到所有ajax请求都完成

来自分类Dev

如何取消任务,但要等到完成?