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.
EjercicioLock/EjercicioLockCuentaBancaria.py

33 lines
758 B

import threading
class CuentaBancaria:
def __init__(self):
self.saldo = 1000
self.lock = threading.Lock()
def retirar_dinero(self, cantidad):
with self.lock:
if self.saldo >= cantidad:
self.saldo -= cantidad
return True
else:
return False
def usuario(cuenta, cantidad):
if cuenta.retirar_dinero(cantidad):
print(f"Retirada exitosa de {cantidad} euros.")
else:
print("Saldo insuficiente.")
cuenta = CuentaBancaria()
usuarios = []
for i in range(10):
t = threading.Thread(target=usuario, args=(cuenta, 200))
usuarios.append(t)
t.start()
for t in usuarios:
t.join()
print(f"Saldo final: {cuenta.saldo} euros.")

Powered by INFORMATICA.FP.EDU.ES.