This is a thread synchronization test and the output I get is this:
The code I am using is this:
compartirinf.java
package cuenta;
public class CompartirInf{ // Esto es el programa principal
public static void main(String[] args){
Cuenta c=new Cuenta(40,500);
Persona p1=new Persona ("Ana",c);
Persona p2=new Persona ("Juan",c);
p1.start();
p2.start();
}
}
cuenta.java
package cuenta;
class Cuenta{
private int saldo;
private int max_saldo;
//constructor
Cuenta (int s,int m)
{
saldo=s; //inicializa el saldo actual
max_saldo=m;
}//--------------------------------------
//devuelve el saldo
int getSaldo()
{
return saldo; //
}
// sumar la cantidad al saldo
void sumar(int cantidad)
{
saldo += cantidad;
}//-----------------------------------
// resta la cantidad al saldo
void restar(int cantidad)
{
saldo=saldo-cantidad;
}//-----------------------------------
//comprueba se pueda retirar dinero y lo retira
public synchronized void Reintegro(int cant,String nom)
{
if (getSaldo()<cant) //si no hay suficiente saldo muestro mensaje
System.out.println (nom +" quiere retirar "+ cant+" dinero no hay saldo suficiente . Saldo Actual: "+ getSaldo() +"");
while (getSaldo()<cant){ //si no hay bastante saldo espero
try {
wait();
}catch (InterruptedException e){};
}
System.out.println (" Se va a retirar saldo (actual es: "+ getSaldo() +")");
try {
Thread.sleep(500);
} catch (InterruptedException ex) {}
restar (cant);
notifyAll();
System.out.println (nom +" retira => : "+cant +". Saldo Actual: "+ getSaldo() +"");
}// fin retirar dinero------------------------------
//comprueba se pueda insertar dinero y lo ingresa
public synchronized void Ingreso(int cant,String nom){
// Si el saldo es menor a la cantidad límite lo ingresa en la cuenta del objeto persona
//max_saldo
if ((cant+getSaldo())<max_saldo) //si no hay suficiente saldo muestro mensaje
System.out.println (nom +" quiere retirar "+ cant+" dinero es superior al saldo máximo permitido . Saldo Actual: "+ getSaldo() +"");
while ((cant+getSaldo())<max_saldo){ //si el saldo a ingresar es superior al saldo máximo
try {
wait();
}catch (InterruptedException e){};
}
System.out.println (" Se va a añadir saldo (actual es: "+ getSaldo() +")");
try {
Thread.sleep(500);
} catch (InterruptedException ex) {}
sumar (cant);
notifyAll();
System.out.println (nom +" añade => : "+cant +". Saldo Actual: "+ getSaldo() +"");
}
}
person.java
package cuenta;
import java.util.Random;
class Persona extends Thread{
private Cuenta c; //declaro objeto cuenta c
private String nom;
int dinero;
//constructor
public Persona (String n, Cuenta c){
// super(n);
this.c=c;
nom=n;
}
//run
public void run(){
Random rnd = new Random();
dinero=(int)(rnd.nextDouble() * 501.0);
c.Ingreso (dinero,nom);
dinero=(int)(rnd.nextDouble() * 501.0);
c.Reintegro(dinero,nom);
dinero=(int)(rnd.nextDouble() * 501.0);
c.Ingreso (dinero,nom);
dinero=(int)(rnd.nextDouble() * 501.0);
c.Reintegro(dinero,nom);
}
}
The purpose is to get an execution where the movements of adding money to the account, not just withdrawal, are also shown. However, it seems that the thread stops at some point and does not show the actions that it is supposed to do.
Any clue as to what could be failing?