简体   繁体   中英

Wait till thread finishes

Main Class:

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

Other classes:

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

After this statement:

me.eat(times,p);

The rest of code is running but I want that after the threads run by food , producer and consumer finished. How can I do this?

You could use a CountDownLatch to allow the producer and consumer to signal when they are done, and wait for that signal in your main code.

Main code :

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

Updated classes :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM