gpt4 book ai didi

java - 线程在它应该醒来的时候没有醒来

转载 作者:行者123 更新时间:2023-12-03 12:58:51 25 4
gpt4 key购买 nike

应用番茄酱线程没有唤醒,我不知道为什么!

我正在学习 Java 线程交互。

我确实使用了notifyAll()方法,但是“applyKetchup”线程无法按时唤醒!

线程应该有足够的时间唤醒,这是令人难以置信的。

预期的结果是

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

实际结果是
makeBread!applyKetchup!makeBread!makeBread!makeBread!makeBread!applyKetchup!applyKetchup!applyKetchup!applyKetchup!

这是我的代码。
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;
}


提前致谢!

最佳答案

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

开/关
制作面包!涂抹番茄酱!制作面包!涂抹番茄酱!制作面包!涂抹番茄酱!制作面包!涂抹番茄酱!制作面包!涂抹番茄酱!

关于java - 线程在它应该醒来的时候没有醒来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com