-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCook.java
More file actions
37 lines (34 loc) · 1.52 KB
/
Cook.java
File metadata and controls
37 lines (34 loc) · 1.52 KB
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
public class Cook implements Runnable {
public boolean continueWorking = true;
@Override
public void run() {
boolean needToWait;
while (continueWorking || needToCookOrders()) {
try {
synchronized (this) {
needToWait = !needToCookOrders();
if (!needToWait) {
cooking();
}
}
if (continueWorking && needToWait) {
System.out.println("Повар отдыхает");
Thread.sleep(100);
}
} catch (InterruptedException e) {
}
}
}
private boolean needToCookOrders() {
return !Manager.getInstance().getOrderQueue().isEmpty();
}
private void cooking() throws InterruptedException {
Manager manager = Manager.getInstance();
Order order = manager.getOrderQueue().poll(); // повар берет заказ из очереди
System.out.println(String.format("Заказ будет готовиться %d мс для стола №%d", order.getTime(), order.getTableNumber()));
Thread.sleep(order.getTime()); // готовим блюдо
Dishes dishes = new Dishes(order.getTableNumber());// это готовое блюдо
manager.getDishesQueue().add(dishes);
System.out.println(String.format("Заказ для стола №%d готов", dishes.getTableNumber()));
}
}