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.
36 lines
835 B
36 lines
835 B
10 months ago
|
import threading
|
||
|
|
||
|
class Cajero:
|
||
|
def __init__(self):
|
||
|
self.saldo = 1000
|
||
|
self.lock = threading.Lock()
|
||
|
|
||
|
|
||
|
def retirar_dinero(self,n):
|
||
|
with self.lock:
|
||
|
if self.saldo >= n:
|
||
|
self.saldo = self.saldo - n
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
cuenta = Cajero()
|
||
|
retiros = []
|
||
|
|
||
|
def realizar_retiro(cuenta, dinero):
|
||
|
if Cajero.retirar_dinero(cuenta, dinero):
|
||
|
print(f"Retiro exitoso. Nuevo saldo: {cuenta.saldo}")
|
||
|
else:
|
||
|
print("Saldo insuficiente")
|
||
|
|
||
|
for i in range(10):
|
||
|
retiro = threading.Thread(target=realizar_retiro, args=(cuenta, 200))
|
||
|
retiros.append(retiro)
|
||
|
|
||
|
for retiro in retiros:
|
||
|
retiro.start()
|
||
|
|
||
|
for retiro in retiros:
|
||
|
retiro.join()
|
||
|
|
||
|
print(f"Saldo final {cuenta.saldo}")
|