-
Notifications
You must be signed in to change notification settings - Fork 0
/
LtP14_threads_example.py
80 lines (74 loc) · 2.69 KB
/
LtP14_threads_example.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# example of threads usage: whenever threads are used
# it is possible to block one of the threads - the
# real world script that can utilize this option
# will cover a modeling of bank account: let's say
# that there is 100$ in the account but there are 3
# different people that can withdraw money from that
# account, the script will block the possibility
# of withdrawing money from the account while
# one of the person is taking the money at a time
import threading
import time
import random
# create a class for bank account with threads
class BankAccount(threading.Thread):
# static variable
accountBalance = 100
# initialize a thread with name and amount of
# money requested
def __init__(self, name, moneyRequest):
threading.Thread.__init__(self)
# assign name of person that want to
# withdraw money
self.name = name
# get the amount of money to be withdrawn
self.moneyRequest = moneyRequest
# run property definition
def run(self):
# create locking property - not to be able
# to acquire the money from other threads
threadLock.acquire()
# access to get the money from account
BankAccount.getMoney(self)
# release the lock for a thread after first
# withdraw to work
threadLock.release()
# create a static method for getting money
@staticmethod
def getMoney(customer):
# printout the msg about who, how much and
# when wants to withdraw money from account
print("{} tries to withdraw {}$ at"
"{}".format(customer.name,
customer.moneyRequest,
time.strftime("%H:%M:%S",
time.gmtime())))
# what to do in case of enough money in account
if BankAccount.accountBalance - customer.moneyRequest > 0:
BankAccount.accountBalance -= customer.moneyRequest
print("New account balance: "
"{}$".format(BankAccount.accountBalance))
# what to do in case there is not enough money
else:
print("Not enough money in account")
print("Current balance: "
"{}$".format(BankAccount.accountBalance))
# time to sleep after execution - needed to see the
# difference in execution of next threads
time.sleep(3)
# thread lock method assigned
threadLock = threading.Lock()
# 3 people wants to take specific amount of money
luke = BankAccount("Luke", 1)
john = BankAccount("John", 100)
jean = BankAccount("Jean", 50)
# start all threads
luke.start()
john.start()
jean.start()
# join all threads
luke.join()
john.join()
jean.join()
# end printout msg
print("Execution Ends")