-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob.py
More file actions
46 lines (34 loc) · 1.13 KB
/
prob.py
File metadata and controls
46 lines (34 loc) · 1.13 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
42
43
44
45
46
import threading
import time
import random
class BankAccount(threading.Thread):
accBalance = 100
def __init__(self, name, moneyRequest):
threading.Thread.__init__(self)
self.name = name
self.moneyRequest = moneyRequest
def run(self):
threadLock.acquire()
BankAccount.getMoney(self)
threadLock.release()
@staticmethod
def getMoney(customer):
print("{} tries to withdraw Ksh {} at {} ".format(customer.name, customer.moneyRequest, time.strftime("%H: %M: %S", time.gmtime())))
if BankAccount.accBalance - customer.moneyRequest > 0:
BankAccount.accBalance -= customer.moneyRequest
print("New acc balance: KSH{}".format(BankAccount.accBalance))
else:
print("not enough money in account")
print("Current balance: KSH{}".format(BankAccount.accBalance))
time.sleep(3)
threadLock = threading.Lock()
doug = BankAccount("doug", 1)
paul = BankAccount("paul", 100)
sally = BankAccount("sally", 50)
doug.start()
paul.start()
sally.start()
doug.join()
paul.join()
sally.join()
print("Execution ends")