简体   繁体   English

线程不应该唤醒

[英]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.我正在学习 Java 线程交互。

I did use the notifyAll() method, but the "applyKetchup" thread couldn't wake up on time!我确实使用了 notifyAll() 方法,但是“applyKetchup”线程无法按时唤醒!

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! O/P makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!makeBread!applyKetchup!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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