-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule10_3.py
More file actions
41 lines (34 loc) · 1.26 KB
/
Copy pathmodule10_3.py
File metadata and controls
41 lines (34 loc) · 1.26 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
from threading import Thread, Lock
from random import randint
from time import sleep
class Bank:
def __init__(self):
self.balance = 0
self.lock = Lock()
def deposit(self):
for i in range(100):
delta = randint(50, 500)
self.balance += delta
print(f"Пополнение #{i}: {delta}. Баланс: {self.balance}")
if self.balance >= 500 and self.lock.locked():
self.lock.release()
sleep(0.001)
def take(self):
for i in range(100):
delta = randint(50, 500)
print(f"Запрос #{i} на {delta}")
if self.balance >= delta:
self.balance -= delta
print(f"Снятие #{i}: {delta}. Баланс: {self.balance}")
else:
print(f"Запрос #{i} отклонён, недостаточно средств")
self.lock.acquire()
bk = Bank()
# Т.к. методы принимают self, в потоки нужно передать сам объект класса Bank
th1 = Thread(target=Bank.deposit, args=(bk,))
th2 = Thread(target=Bank.take, args=(bk,))
th1.start()
th2.start()
th1.join()
th2.join()
print(f'Итоговый баланс: {bk.balance}')