[英]Synchronization between two lists
我有以下使用 java 並發的問題。
一家餐廳有 3 位客戶和炊具。 它們都通過 2 個列表更改信息。 一份用於下訂單的清單,另一份用於下餐的清單。 客戶將訂單發送到訂單列表,炊具讀取。 炊具將准備好的餐點發送給餐單上的客戶。 我將炊具和客戶實現為處理 2 個列表的線程。 此問題的目標是同步對列表的訪問。 不幸的是,線程之間的wait()
和notifyAll()
順序不好,我沒有得到什么問題。 我的代碼如下。 誰能指出我的代碼有什么問題?
編輯
問題是客戶端和炊具在沒有吃完所有食物的情況下掛起,正如我們從 output 中看到的那樣。
Tony: Client orders.
Tony: The meal is still not ready.
Cooker: Cooking Fish soup for Tony
Cooker: The cooker will serve the meal Fish soup for Tony
Cooker: Waiting for orders to cook.
餐廳.java
import java.util.ArrayList;
import java.util.List;
public class Restaurant
{
public static void main(String[] args)
{
List<Order> orderQueue = new ArrayList<>();
List<Dish> dishesQueue = new ArrayList<>();
// Client 1
String clientName1 = "Tony";
List<Order> personalOrders = new ArrayList<>();
personalOrders.add(new Order(clientName1, "Fish soup"));
personalOrders.add(new Order(clientName1, "Duck rice"));
personalOrders.add(new Order(clientName1, "Coffee"));
personalOrders.add(new Order(clientName1, "Pudding"));
Thread tclient1 = new Thread(new Client(personalOrders, orderQueue, dishesQueue), clientName1);
// Client 2
String clientName2 = "John";
personalOrders = new ArrayList<>();
personalOrders.add(new Order(clientName2, "Stew"));
personalOrders.add(new Order(clientName2, "Cake"));
Thread tclient2 = new Thread(new Client(personalOrders, orderQueue, dishesQueue), clientName2);
// Client 3
String clientName3 = "Mike";
personalOrders = new ArrayList<>();
personalOrders.add(new Order(clientName3, "Hotdog"));
personalOrders.add(new Order(clientName3, "Coffee"));
Thread tclient3 = new Thread(new Client(personalOrders, orderQueue, dishesQueue), clientName3);
Thread tCooker = new Thread(new Chef(orderQueue, dishesQueue), "Cooker");
tclient1.start();
tclient2.start();
tclient3.start();
tCooker.start();
}
}
廚師java
import java.util.List;
class Chef implements Runnable
{
private final List<Order> orders;
private final List<Dish> dishes;
public Chef(List<Order> sharedOrders, List<Dish> sharedDishes)
{
this.orders = sharedOrders;
this.dishes = sharedDishes;
}
@Override
public void run()
{
while (true)
{
try
{
Order order = cook();
serve(order);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
private Order cook() throws InterruptedException {
Order order = null;
synchronized (orders) {
if (orders.isEmpty()) {
System.out.println(Thread.currentThread().getName() + ":\t Waiting for orders to cook.");
Thread.sleep(1000);
orders.wait();
} else {
order = orders.remove(0);
System.out.println(Thread.currentThread().getName() + ":\t Cooking " + order.getDish() + " for " + order.getClientName());
orders.notifyAll();
}
}
return order;
}
private void serve(Order order) throws InterruptedException
{
synchronized (dishes)
{
if (dishes.isEmpty()) {
System.out.println(Thread.currentThread().getName() + ":\t The cooker is waiting for more requests");
dishes.wait();
}
// else {
Thread.sleep(1000);
Dish meal = new Dish(order.getClientName(), order.getDish());
System.out.println(Thread.currentThread().getName() + ":\t The cooker will serve the meal " + meal.getDish() + " for " + meal.getClientName());
dishes.add(meal);
dishes.notifyAll();
// }
}
}
}
客戶端.java
import java.util.Collections;
import java.util.List;
class Client implements Runnable
{
private final List<Order> orders;
private final List<Order> personalOrders;
private final List<Dish> dishes;
public Client(List<Order> personalOrders, List<Order> orders, List<Dish> dishes)
{
this.personalOrders = personalOrders;
this.orders = orders;
this.dishes = dishes;
}
@Override
public void run()
{
while (!personalOrders.isEmpty())
{
try
{
request(getPersonalOrders().remove(0));
consume();
} catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ":\t Acabou de comer.");
}
private void request(Order order) throws InterruptedException {
synchronized (orders) {
System.out.println(Thread.currentThread().getName() + ":\t Cliente faz pedido");
orders.add(order);
Thread.sleep(1000);
orders.notifyAll();
}
}
private void consume() throws InterruptedException
{
synchronized (dishes)
{
if (dishes.isEmpty())
{
System.out.println(Thread.currentThread().getName() + ":\t The meal is still not ready.");
dishes.wait();
}
// else {
Thread.sleep(1000);
Dish meal = dishes.remove(0);
System.out.println(Thread.currentThread().getName() + ":\t " + meal.getClientName() + " ready to eat " + meal.getDish());
dishes.notifyAll();
// }
}
}
public List<Order> getPersonalOrders() {
return personalOrders;
}
}
這個說法:
this.dishes = Collections.synchronizedList(dishes);
使this.dishes
與 discs 不同dishes
。 在其中一個對象上同步和通知不會影響另一個 object。
這就是為什么您的客戶永遠不會收到關於准備好的菜餚的通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.