Skip to content

Commit 98d865b

Browse files
authored
Final QA (#595)
1 parent 2c30025 commit 98d865b

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

thread-safety-locks/bank_barrier.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
teller_barrier = threading.Barrier(3)
77

88

9+
def now():
10+
return time.strftime("%H:%M:%S")
11+
12+
913
def prepare_for_work(name):
10-
print(f"{int(time.time())}: {name} is preparing their counter.")
14+
print(f"{now()}: {name} is preparing their counter.")
1115

1216
# Simulate the delay to prepare the counter
1317
time.sleep(random.randint(1, 3))
14-
print(f"{int(time.time())}: {name} has finished preparing.")
18+
print(f"{now()}: {name} has finished preparing.")
1519

1620
# Wait for all tellers to finish preparing
1721
teller_barrier.wait()
18-
print(f"{int(time.time())}: {name} is now ready to serve customers.")
22+
print(f"{now()}: {name} is now ready to serve customers.")
1923

2024

2125
tellers = ["Teller 1", "Teller 2", "Teller 3"]
@@ -24,4 +28,4 @@ def prepare_for_work(name):
2428
for teller_name in tellers:
2529
executor.submit(prepare_for_work, teller_name)
2630

27-
print(f"{int(time.time())}: All tellers are ready to serve customers.")
31+
print(f"{now()}: All tellers are ready to serve customers.")

thread-safety-locks/bank_condition.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@
99
customer_queue = []
1010

1111

12+
def now():
13+
return time.strftime("%H:%M:%S")
14+
15+
1216
def serve_customers():
1317
while True:
1418
with customer_available_condition:
1519
# Wait for a customer to arrive
1620
while not customer_queue:
17-
print(f"{int(time.time())}: Teller is waiting for a customer.")
21+
print(f"{now()}: Teller is waiting for a customer.")
1822
customer_available_condition.wait()
1923

2024
# Serve the customer
2125
customer = customer_queue.pop(0)
22-
print(f"{int(time.time())}: Teller is serving {customer}.")
26+
print(f"{now()}: Teller is serving {customer}.")
2327

2428
# Simulate the time taken to serve the customer
25-
time.sleep(random.randint(1, 3))
26-
print(f"{int(time.time())}: Teller has finished serving {customer}.")
29+
time.sleep(random.randint(1, 5))
30+
print(f"{now()}: Teller has finished serving {customer}.")
2731

2832

2933
def add_customer_to_queue(name):
3034
with customer_available_condition:
31-
print(f"{int(time.time())}: {name} has arrived at the bank.")
35+
print(f"{now()}: {name} has arrived at the bank.")
3236
customer_queue.append(name)
3337

3438
customer_available_condition.notify()
@@ -43,11 +47,8 @@ def add_customer_to_queue(name):
4347
]
4448

4549
with ThreadPoolExecutor(max_workers=6) as executor:
46-
4750
teller_thread = executor.submit(serve_customers)
48-
4951
for name in customer_names:
5052
# Simulate customers arriving at random intervals
51-
time.sleep(random.randint(2, 5))
52-
53+
time.sleep(random.randint(1, 3))
5354
executor.submit(add_customer_to_queue, name)

thread-safety-locks/bank_deadlock.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self):
1111
def deposit(self, amount):
1212
print(
1313
f"Thread {threading.current_thread().name} waiting "
14-
f"to acquire lock for deposit()"
14+
"to acquire lock for deposit()"
1515
)
1616
with self.lock:
1717
print(
@@ -24,7 +24,7 @@ def deposit(self, amount):
2424
def _update_balance(self, amount):
2525
print(
2626
f"Thread {threading.current_thread().name} waiting to acquire "
27-
f"lock for _update_balance()"
27+
"lock for _update_balance()"
2828
)
2929
with self.lock: # This will cause a deadlock
3030
print(
@@ -36,16 +36,11 @@ def _update_balance(self, amount):
3636

3737
account = BankAccount()
3838

39-
40-
def make_deposit():
41-
account.deposit(100)
42-
43-
4439
with ThreadPoolExecutor(
4540
max_workers=3, thread_name_prefix="Worker"
4641
) as executor:
4742
for _ in range(3):
48-
executor.submit(make_deposit)
43+
executor.submit(account.deposit, 100)
4944

5045

5146
print(f"Final balance: {account.balance}")

thread-safety-locks/bank_multithreaded_withdrawal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def withdraw(self, amount):
1212
time.sleep(0.1) # Simulate a delay
1313
self.balance = new_balance
1414
else:
15-
raise Exception("Insufficient balance")
15+
raise ValueError("Insufficient balance")
1616

1717

1818
account = BankAccount()

thread-safety-locks/bank_rlock.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,11 @@ def _update_balance(self, amount):
3636

3737
account = BankAccount()
3838

39-
40-
def make_deposit():
41-
account.deposit(100)
42-
43-
4439
with ThreadPoolExecutor(
4540
max_workers=3, thread_name_prefix="Worker"
4641
) as executor:
4742
for _ in range(3):
48-
executor.submit(make_deposit)
43+
executor.submit(account.deposit, 100)
4944

5045

5146
print(f"Final balance: {account.balance}")

thread-safety-locks/bank_semaphore.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
teller_semaphore = threading.Semaphore(2)
88

99

10+
def now():
11+
return time.strftime("%H:%M:%S")
12+
13+
1014
def serve_customer(name):
11-
print(f"{int(time.time())}: {name} is waiting for a teller.")
15+
print(f"{now()}: {name} is waiting for a teller.")
1216
with teller_semaphore:
13-
print(f"{int(time.time())}: {name} is being served by a teller.")
17+
print(f"{now()}: {name} is being served by a teller.")
1418
# Simulate the time taken for the teller to serve the customer
1519
time.sleep(random.randint(1, 3))
16-
print(f"{int(time.time())}: {name} is done being served.")
20+
print(f"{now()}: {name} is done being served.")
1721

1822

1923
customers = [
@@ -29,4 +33,4 @@ def serve_customer(name):
2933
thread = executor.submit(serve_customer, customer_name)
3034

3135

32-
print("All customers have been served.")
36+
print(f"{now()}: All customers have been served.")

thread-safety-locks/bank_thread_safe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def withdraw(self, amount):
1616
time.sleep(0.1) # Simulate a delay
1717
self.balance = new_balance
1818
else:
19-
raise Exception("Insufficient balance")
19+
raise ValueError("Insufficient balance")
2020

2121
def deposit(self, amount):
2222
with self.account_lock:
@@ -29,7 +29,6 @@ def deposit(self, amount):
2929
account = BankAccount(1000)
3030

3131
with ThreadPoolExecutor(max_workers=3) as executor:
32-
3332
executor.submit(account.withdraw, 700)
3433
executor.submit(account.deposit, 1000)
3534
executor.submit(account.withdraw, 300)

0 commit comments

Comments
 (0)