-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restaurant.java
60 lines (54 loc) · 1.72 KB
/
Restaurant.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
class Restaurant {
private String orderName;
private boolean isOrderReady;
public synchronized void foodOrder(String orderName) {
while (!isOrderReady) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Waiter received the order: " + orderName);
isOrderReady = false;
notifyAll();
}
public synchronized void cookedOrder(String orderName) {
while (isOrderReady) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Chef has received the order: " + orderName);
System.out.println("Order is being prepared...");
isOrderReady = true;
notifyAll();
}
public synchronized void receivedOrder(String orderName) {
while (isOrderReady) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Customer received the order: " + orderName);
isOrderReady = true;
notifyAll();
}
}
public class Main {
public static void main(String[] args) {
Restaurant rst = new Restaurant();
Thread waiter = new Thread(() -> { rst.foodOrder("Burger");}, "Waiter");
Thread cook = new Thread(() -> { rst.cookedOrder("Burger");}, "Chef");
Thread customer = new Thread(() -> { rst.receivedOrder("Burger");}, "Customer");
waiter.start();
cook.start();
customer.start();
}
}
*/