-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrace_condition.py
More file actions
28 lines (20 loc) · 826 Bytes
/
Copy pathrace_condition.py
File metadata and controls
28 lines (20 loc) · 826 Bytes
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
import threading
available_seats = 2
class Movie:
def booking(self, number_of_seats):
global available_seats
if number_of_seats <= available_seats:
print(f'{number_of_seats} seats booked successfully for {threading.current_thread().name} \n')
available_seats -= number_of_seats
elif number_of_seats > available_seats:
print(f'{threading.current_thread().name}, number of seats that you want is greater than available seats')
else:
print('All seats are sold out')
movie = Movie()
threads = [
threading.Thread(target=movie.booking, args=(2,), name='Mohammad'),
threading.Thread(target=movie.booking, args=(2,), name='Ali'),
]
[t.start() for t in threads]
[t.join() for t in threads]
print(f'Available seats: {available_seats}')