简体   繁体   中英

Thread not waking up when it should

The apply ketchup thread doesn't wake up and I don't know why!

I'm learning about Java thread interaction.

I did use the notifyAll() method, but the "applyKetchup" thread couldn't wake up on time!

The thread should have enough time to wake up,it's unbelievable.

The expected result is

makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!

The actual result is

makeBread!applyKetchup!makeBread!makeBread!makeBread!makeBread!applyKetchup!applyKetchup!applyKetchup!applyKetchup!

this is my code.

import java.util.ArrayList;

public class ProduceHamburgers {
    public static void main(String[] args) {
        HamburgerFactory hamburgerFactory = new HamburgerFactory();
        hamburgerFactory.delivery(5);
    }
}

class HamburgerFactory {

    private ArrayList<Hamburger> hamburgers = new ArrayList<>();
    private ArrayList<Hamburger> breads = new ArrayList<>();

    public void delivery(int amount) {
        for (int index = 0; index < amount; index++) {
            new Thread(() -> applyKetchup(), "applyKetchup-" + index).start();
        }
        for (int index = 0; index < amount; index++) {
            new Thread(() -> makeBread(), "makeBread-" + index).start();
        }
    }

    private synchronized void applyKetchup() {
        while (breads.isEmpty()) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Hamburger current = breads.get(0);
        breads.remove(current);
        current.hadKetchup = true;
        System.out.print("applyKetchup!");
        hamburgers.add(current);
    }

    private synchronized void makeBread() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        breads.add(new Hamburger());
        System.out.print("makeBread!");
        this.notifyAll();
    }

}

class Hamburger {
    public boolean hadKetchup = false;
}

thanks in advance!

import java.util.ArrayList;

public class ProduceHamburgers {
    public static void main(String[] args) {
        HamburgerFactory hamburgerFactory = new HamburgerFactory();
        hamburgerFactory.delivery(5);
    }
}

class HamburgerFactory {

    private ArrayList<Hamburger> hamburgers = new ArrayList<>();
    private ArrayList<Hamburger> breads = new ArrayList<>();

    public void delivery(int amount) {
        for (int index = 0; index < amount; index++) {
            new Thread(() -> applyKetchup(), "applyKetchup-" + index).start();
        }
        for (int index = 0; index < amount; index++) {
            new Thread(() -> makeBread(), "makeBread-" + index).start();
        }
    }

    private synchronized void applyKetchup() {
        while (breads.isEmpty()) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Hamburger current = breads.get(0);
        breads.remove(current);
        current.hadKetchup = true;
        System.out.print("applyKetchup!");
        hamburgers.add(current);
        this.notifyAll();
    }

    private synchronized void makeBread() {
         while (!breads.isEmpty()) {
             try {
                 this.wait();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
        breads.add(new Hamburger());
        System.out.print("makeBread!");
        this.notifyAll();
    }

}

class Hamburger {
    public boolean hadKetchup = false;
}

O/P makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!

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