commit
6b0c1e8be8
@ -0,0 +1,33 @@
|
||||
from threading import Thread, Lock
|
||||
|
||||
class atm:
|
||||
def __init__(self):
|
||||
self.balance = 1000
|
||||
self.lock = Lock()
|
||||
|
||||
def takeMoneyOut(self, quantity):
|
||||
with self.lock:
|
||||
if self.balance >= quantity:
|
||||
self.balance -= quantity
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def user(atm, amount):
|
||||
if atm.takeMoneyOut(amount):
|
||||
print("Taken out: ", amount)
|
||||
else:
|
||||
print("Not enough balance")
|
||||
|
||||
theAtm = atm()
|
||||
users = []
|
||||
|
||||
for i in range(10):
|
||||
t = Thread(target=user, args=(theAtm, 200))
|
||||
users.append(t)
|
||||
t.start()
|
||||
|
||||
for t in users:
|
||||
t.join()
|
||||
|
||||
print("Final balance: ", theAtm.balance)
|
Loading…
Reference in new issue