From b19be1dbe2fd12ab0b0b59d85b8441522362c263 Mon Sep 17 00:00:00 2001 From: LuisCorGon Date: Fri, 24 Nov 2023 18:24:26 +0100 Subject: [PATCH] EjercicioLock terminado --- EjercicioLock.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 EjercicioLock.py diff --git a/EjercicioLock.py b/EjercicioLock.py new file mode 100644 index 0000000..3f897cd --- /dev/null +++ b/EjercicioLock.py @@ -0,0 +1,36 @@ +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}") \ No newline at end of file