You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
704 B
34 lines
704 B
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)
|