-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathles_38.py
58 lines (48 loc) · 1.84 KB
/
les_38.py
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
import threading
import time
class Table:
def __init__(self, number):
self.number = number
self.is_busy = False
class Cafe:
def __init__(self, tables):
self.queue = []
self.tables = tables
def customer_arrival(self):
customer_number = 1
while customer_number <= 15:
time.sleep(1)
print(f"Посетитель номер {customer_number} прибыл.")
customer = Customer(customer_number, self)
customer.start()
customer_number += 1
def serve_customer(self, customer):
for table in self.tables:
if not table.is_busy:
table.is_busy = True
print(f"Посетитель номер {customer.number} сел за стол {table.number}.")
time.sleep(5)
print(f"Посетитель номер {customer.number} покушал и ушёл.")
table.is_busy = False
return
self.queue.append(customer)
print(f"Посетитель номер {customer.number} ожидает свободный стол.")
class Customer(threading.Thread):
def __init__(self, number, cafe):
threading.Thread.__init__(self)
self.number = number
self.cafe = cafe
def run(self):
self.cafe.serve_customer(self)
# Создаем столики в кафе
table1 = Table(1)
table2 = Table(2)
table3 = Table(3)
tables = [table1, table2, table3]
# Инициализируем кафе
cafe = Cafe(tables)
# Запускаем поток для прибытия посетителей
customer_arrival_thread = threading.Thread(target=cafe.customer_arrival)
customer_arrival_thread.start()
# Ожидаем завершения работы прибытия посетителей
customer_arrival_thread.join()