Repository navigation
Expand file tree
/
Copy pathRestaurantTest.java
More file actions
58 lines (48 loc) · 1.75 KB
/
Copy pathRestaurantTest.java
File metadata and controls
58 lines (48 loc) · 1.75 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Restaurant {
private int availableSeats;
public Restaurant(int seats) {
this.availableSeats = seats;
}
public synchronized void takeSeat(String customerName) {
if (availableSeats > 0) {
availableSeats--;
System.out.println(customerName + " took a seat. Available seats: " + availableSeats);
} else {
System.out.println("No available seats for " + customerName);
}
}
public synchronized void placeOrder(String customerName, String order) {
System.out.println(customerName + " placed order: " + order);
}
public synchronized void leaveSeat(String customerName) {
availableSeats++;
System.out.println(customerName + " left the seat. Available seats: " + availableSeats);
}
}
class Customer implements Runnable {
private Restaurant restaurant;
private String name;
private String order;
public Customer(Restaurant restaurant, String name, String order) {
this.restaurant = restaurant;
this.name = name;
this.order = order;
}
@Override
public void run() {
restaurant.takeSeat(name);
restaurant.placeOrder(name, order);
restaurant.leaveSeat(name);
}
}
public class RestaurantTest {
public static void main(String[] args) {
Restaurant restaurant = new Restaurant(5);
Thread customer1 = new Thread(new Customer(restaurant, "Alice", "Pizza"));
Thread customer2 = new Thread(new Customer(restaurant, "Bob", "Burger"));
Thread customer3 = new Thread(new Customer(restaurant, "Charlie", "Salad"));
customer1.start();
customer2.start();
customer3.start();
}
}