gpt4 book ai didi

java - 执行等待和通知时返回意外结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:14 25 4
gpt4 key购买 nike

我制作了一个等待和通知示例程序,它是一家蛋糕店的抽象。有线程角色蛋糕机生产蛋糕,线程角色服务员送蛋糕。我的期望是每次 CakeMachine 类完成制作蛋糕时,它都会向 Waiter 类发送通知以进行交付。当我运行一个生产 3 个蛋糕的程序时,结果显示只交付了一个蛋糕。这是我的代码:

用于创建蛋糕对象的 Cake 类:

class Cake {
private int weight;
private String color;

public Cake(int weight, String color) {
this.weight = weight;
this.color = color;
}

public String toString(){
return "The cake is in " + color +" and is " + weight + " gram ";

}
}

制作蛋糕的CakeMachine类:

class CakeMachine implements Runnable{


private List<Cake> listCake;

CakeMachine(List<Cake> listCake) {
this.listCake = listCake;
}

public void makeCake() {
int weight = new Random().nextInt(20);
Cake cake = new Cake(weight, "color code is " + weight );
listCake.add(cake);
System.out.println("cake has been cooked ");
listCake.notify();
}

@Override
public void run() {

for (int i = 0; i < 3; i++) {
synchronized (listCake) {
makeCake();
}
}
}
}

送蛋糕的Waiter类:

class Waiter implements Runnable {

private List<Cake> listCake;

Waiter(List<Cake> listCake) {
this.listCake = listCake;
}

public void delivery() {
System.out.println("Waiter is waiting for the cake ");

try {
listCake.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
Cake cake = listCake.get(listCake.size() - 1);
System.out.println(cake.toString() + "has been delivered to customers ");
}

@Override
public void run() {

for (int i = 0; i < 3; i++)
{
synchronized (listCake) {
delivery();
}
}
}
}

还有主类:

public class WaitAndNotify {

public static List<Cake> listCake = new ArrayList<>();

public static void main(String[] args) {

Thread waiter = new Thread(new Waiter(listCake));
Thread cakeMachine = new Thread(new CakeMachine(listCake));
waiter.start();
cakeMachine.start();

}
}

程序运行结果为:

Waiter is waiting for the cake 
cake has been cooked
cake has been cooked
cake has been cooked
The cake is in color code is 18 and is 18 gram has been delivered to customers
Waiter is waiting for the cake

请帮助我了解这种情况。

最佳答案

两大要点:

  • 您无法控制哪个线程何时运行。

  • 如果没有其他线程恰好在该监视器上等待,则通知无效。您的服务员停止等待,蛋糕制作可以继续进行,并在服务员再次获得显示器之前完成。

在您的示例中,调度程序决定先运行服务员的一次迭代,然后运行蛋糕制作的所有 3 次迭代,然后在制作完所有蛋糕后运行接下来的两次等待迭代。你可以引入标志来指示蛋糕何时准备好以及服务员何时等待,并让蛋糕制作延迟通知直到服务员出现,但试图让线程同步执行确实是一个错误,它破坏了将 Activity 分离到它们自己的线程中的目的。

添加了条件标志后,交付方式将类似于:

public void delivery() {
System.out.println("Waiter is waiting for the cake ");
waiterInPosition = true;
try {
while (!(cakeQueued)) {
listCake.wait();
}
waiterInPosition = false;
cakeQueued = false;
Cake cake = listCake.get(listCake.size() - 1);
System.out.println(cake.toString()
+ "has been delivered to customers ");
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // restore interrupt flag
}
}

但这是一个玩具示例,非常尴尬。将其建模为生产者-消费者问题会更自然,其中蛋糕制造商将蛋糕添加到队列中,而服务员从队列中取出蛋糕。您可以尝试制作自己的阻塞队列,您的队列方法可以处理等待和通知——让您的数据结构处理同步和阻塞比让工作任务来处理要自然得多。这将向您传授与当前示例一样多的有关使用等待和通知的知识,同时更能代表现实世界的并发模式。

Oracle tutorial on guarded blocks包括一个阻塞队列示例,关于在带有条件变量的循环中等待的建议是必不可少的,请阅读它。

关于java - 执行等待和通知时返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36169376/

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