各顧客の注文を登録する必要があります ウェイターは顧客のすべてのリクエストを書き留めた後にのみ注文を送信できます。要求がJavaのスレッドを同期させる方法
を配信されるまで お客様は、私はウェイター
とクライアントのアクションを同期することはできません私は私のオブジェクトのお客様からの各アクションを同期させる必要があるとウェイター お客様が1オーダー ウェイターがNの顧客に
を持っている待機する必要があります一連の動作は、私が楽しんだと言わなければならない
package room;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Customer extends Thread {
private Random random;
private int id;
private static int ID;
private Order order;
private Waiter waiter;
public Customer(Waiter garcom) {
this.random = new Random();
this.id = ID + 1;
Customer.ID++;
this.waiter = garcom;
}
@Override
public void run() {
orderRequest();
waitOrder();
receiveRequest();
consumer();
}
public synchronized void orderRequest() {
synchronized (this.order) {
int r = random.nextInt(3);
switch (r) {
case 0:
this.order.setOrder("Food");
break;
case 1:
this.order.setOrder("Drink");
break;
default:
this.order.setOrder("Help");
break;
}
System.out.println(this.toString() + " request " + this.order.getOrder() + "to " + this.waiter.toString());
this.order.notify();
try {
this.order.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public synchronized void receiveRequest() {
synchronized (this.order) {
this.order.notify();
try {
this.order.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(this + " has received " + this.order + " from " + this.waiter);
}
}
private void waitOrder() {
synchronized (this.order) {
System.out.println(this + " consumer " + this.order);
this.order.notify();
try {
this.order.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public synchronized void consumer() {
synchronized (this.order) {
System.out.println(this + " was consumed " + this.order);
this.order.notify();
try {
this.order.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void setOrder(Order order) {
this.order = order;
}
public Order getOrder() {
return order;
}
@Override
public String toString() {
return "Customer: " + id;
}
}
package room;
public class Order {
private int orderNumber;
private static int ORDER_NUMBER = 0;
private Customer customer;
private String order;
public Order(Customer c) {
this.customer = c;
this.orderNumber = ORDER_NUMBER + 1;
this.customer.setOrder(this);
Order.ORDER_NUMBER++;
}
public String getOrder() {
return order;
}
public int getOrderNumber() {
return orderNumber;
}
public void setOrder(String order) {
this.order = order;
}
public Customer getCustomer() {
return customer;
}
@Override
public String toString() {
return "Order: " + order + " Nº " + orderNumber;
}
}
package room;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Waiter extends Thread {
private Saloon saloon;
private int id;
private static int ID = 0;
private List<Order> orders;
public Waiter(Saloon bar) {
this.saloon = bar;
this.id = ID + 1;
Waiter.ID++;
this.orders = new ArrayList<>();
}
@Override
public void run() {
while (saloon.isOpen()) {
registerOrder();
deliveryRequest();
saloon.round();
}
}
public synchronized void registerOrder() {
for (Order order : orders) {
synchronized (order) {
order.notify();
try {
order.wait();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println(this.toString() + " "
+ "record " + order.toString()
+ " to " + order.getCustomer());
}
}
}
public synchronized void deliveryRequest() {
for (Order order : orders) {
synchronized (order) {
order.notify();
try {
order.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(this.toString() + " "
+ "delivered " + order.toString()
+ " to " + order.getCustomer());
}
}
}
public synchronized void recordOrder(Order order) {
synchronized (orders) {
this.orders.add(order);
}
}
public List<Order> getOrders() {
return orders;
}
public Saloon getSaloon() {
return saloon;
}
@Override
public String toString() {
return "Waiter: " + id;
}
}
package room;
import java.util.ArrayList;
import java.util.List;
public class Saloon {
private int maxRound;
private int numGarcons;
private volatile int round;
private int capacity;
private int customerCount;
private final List<Waiter> waiters;
public Saloon() {
this.waiters = new ArrayList<>();
this.round = 1;
}
public Saloon(int customerCount, int waiterCount,
int capacity, int rounds) {
this();
this.numGarcons = waiterCount;
this.customerCount = customerCount;
this.capacity = capacity;
this.maxRound = rounds;
}
/**
* Should it be call each round
*/
public void openBar() {
this.capacity = this.customerCount/this.capacity;
System.out.println("Round " + this.round);
for (int i = 0; i < this.numGarcons; i++) {
//Create a waiter
Waiter g = new Waiter(this);
for (int j = 0; j < this.capacity; j++) {
//create customer
Customer c = new Customer(g);
//an order
Order p = new Order(c);
//register order
g.recordOrder(p);
//call thread client
c.start();
}
//waiter serves one client at a time
g.start();
this.waiters.add(g);
}
}
public boolean isOpen() {
if (this.round < this.maxRound) {
return true;
}
return false;
}
public void round() {
this.round++;
}
}
注文はウェイターのために設定されることはありませんように見えスタータのためのようにします。顧客の注文を作成してオーダーリストに設定しましたが、いつそれをウェイターに渡しますか? – Maxs728
ちょうど私はコード全体を見ていると知っているし、修正を働いている – Maxs728
これは[スリーピング・バーバーの問題](http://web.cecs.pdx.edu/~harry/Blitz/OSProject/p3 /SleepingBarberProblem.pdf)、[wiki](https://en.wikipedia.org/wiki/Sleeping_barber_problem)比喩は、Waiterは理髪師に似ており、CustomerタスクはHaircutからFood orderに変更されています。軽微な違いがありますが、[こちら](https://www.cs.helsinki.fi/u/kerola/rio/ohjeet/Java/semaphores/s06e-huhtamaki-merikanto-nevalainen/SleepingBarber.java)から始めることができます。 – vsnyc