[英]How to sync account in Java with two persons
您有1個賬戶,由2個人共享,他們需要在同一個賬戶中提取和存款,存款和提取的方法必須同步,賬戶的最大現金必須是500歐元,最低現金必須是1歐元,該應用程序必須只有 3 個類 Persona、Cuenta、BPA。
如果一個Thread不能存取款必須使用wait,等待另一個線程,並使用notify All,當Thread停止時應該繼續,app需要運行直到兩個Thread都不能存取款。
線程不會在等待時停止,或者只是執行 2 個打印每個線程和應用程序仍在運行但什么都不做,我真的不知道該怎么做,我真的用了 5 個小時的代碼卻什么都不懂
package BPA;
// @author Enrique
import java.util.logging.Level;
import java.util.logging.Logger;
public class BPA {
public static void main(String[] args) {
Cuenta laCuenta = new Cuenta(40, 500);
Persona Ramon = new Persona("Ramon", laCuenta);
Persona Quique = new Persona("Quique", laCuenta);
Quique.start();
Ramon.start();
}// END MAIN
}//END CLASS BPA
package BPA;
// @author Enrique
import java.util.logging.Level;
import java.util.logging.Logger;
public class Persona extends Thread {
String nombre;
private Cuenta cuenta;
private static Object cerrojo = new Object();
public Persona(String nombre, Cuenta cuenta) {
this.nombre = nombre;
this.cuenta = cuenta;
}
@Override
public void run() {
synchronized (cerrojo) {
while (true) {
cuenta.deposit(nombre, cuenta);
cuenta.extract(nombre, cuenta);
try {
// if(Persona.interrupted()){
cerrojo.wait();
System.out.println("STOPPED");
// }
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I'M THE THREAD: " + nombre);
cerrojo.notifyAll();
}
} // END SYNC
}// END RUN
}// END CLASS PERSONA
package BPA;
// @author Enrique
import java.util.Random;
public class Cuenta extends Thread {
private int saldo;
private int saldoMax = 500;
private int saldoMin = 1;
public Cuenta(int saldo, int saldoMax) {
this.saldo = saldo;
this.saldoMax = saldoMax;
}
public synchronized void extract(String nombre, Cuenta cuenta) {
// while (true) {
int dinero;
Random rnd = new Random();
dinero = (int) (rnd.nextDouble() * 250.0);
if ((saldo - dinero) > saldoMin) {
this.saldo = this.saldo - dinero;
System.out.println("Name: " + nombre + " extract cash: " + dinero + " TOTAL CASH: " + getSaldo());
} else {
System.out.println("There isn't enough cash " + nombre + " tryed to extract: " + dinero + " Cash now: " + getSaldo());
// try {
// miCuenta.wait();
// System.out.println("SE PARA EN EL WAIT DE QUITAR");
// } catch (Exception e) {
// System.out.println("SALE POR EL CATCH WAIT de QUITAR");
// }
try {
sleep(500);
} catch (Exception e) {
System.out.println(" SLEEP QUITAR");
}
}
// miCuenta.notify();
// } //END while
}// END METHOD QUITAR
public synchronized void deposit(String nombre, Cuenta cuenta) {
// while (true) {
int dinero;
Random rnd = new Random();
dinero = (int) (rnd.nextDouble() * 250.0);
if ((saldo + dinero) < saldoMax) {
this.saldo = this.saldo + dinero;
System.out.println("Name: " + nombre + " deposit cash: " + dinero + " TOTAL CASH: " + getSaldo());
} else {
System.out.println("The account can't have more than 500 Euros. " + nombre + " tryed to deposit: " + dinero + " Cash now: " + getSaldo());
// try {
// miCuenta.wait();
// System.out.println(" WAIT PONER");
// } catch (Exception e) {
// System.out.println(" CATCH WAIT PONER");
// }
try {
sleep(500);
} catch (Exception e) {
System.out.println(" SLEEP PONER");
}
}
// miCuenta.notify();
// }// END while
}// END METHOD PONER
public int getSaldo() {
return saldo;
}
}// END CLASS CUENTA
我無法讓這個應用程序按照我的意願運行。
錯誤是,我試圖在錯誤的 class 中等待和通知,Threads 需要在方法 deposit 和 extract 中同步,以進行控制。 如果有人需要,我把代碼留在這里。
package BPA;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BPA {
public static void main(String[] args) {
Cuenta laCuenta = new Cuenta(40, 500);
Persona Ramon = new Persona("Ramon", laCuenta);
Persona Quique = new Persona("Quique", laCuenta);
Quique.start();
Ramon.start();
try {
Quique.join();
Ramon.join();
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
}// END MAIN
}//END CLASS BPA
package BPA;
// @author Enrique
import java.util.logging.Level;
import java.util.logging.Logger;
public class Persona extends Thread {
String nombre;
private Cuenta cuenta;
public Persona(String nombre, Cuenta cuenta) {
this.nombre = nombre;
this.cuenta = cuenta;
}
@Override
public synchronized void run() {
while (true) {
cuenta.ingreso(nombre, cuenta);
cuenta.retiro(nombre, cuenta);
try {
sleep(1000);
} catch (Exception e) {
System.out.println(" SLEEP QUITAR");
}
notifyAll();
}// END WHILE
}// END RUN
} // END CLASS PERSONA
package BPA;
// @author Enrique
import java.util.Random;
public class Cuenta {
private int saldo;
private int saldoMax = 500;
private int saldoMin = 1;
public Cuenta(int saldo, int saldoMax) {
this.saldo = saldo;
this.saldoMax = saldoMax;
}
public synchronized void retiro(String nombre, Cuenta cuenta) {
int dinero;
Random rnd = new Random();
dinero = (int) (rnd.nextDouble() * 350.0);
if ((saldo - dinero) > saldoMin) {
this.saldo = this.saldo - dinero;
System.out.println("Name: " + nombre + " extract cash: " + dinero + " TOTAL CASH: " + getSaldo());
notifyAll();
} else {
System.out.println(" **** There isn't enough cash " + nombre + " tryed to extract: " + dinero + " Cash now: " + getSaldo() + " ****");
System.out.println(" ---- " + nombre + " IS WAITING TO EXTRACT MORE MONEY ---- ");
try {
wait();
} catch (Exception e) {
System.out.println("SALE POR EL CATCH WAIT de QUITAR");
}
}
}// END METHOD QUITAR
public synchronized void ingreso(String nombre, Cuenta cuenta) {
int dinero;
Random rnd = new Random();
dinero = (int) (rnd.nextDouble() * 350.0);
if ((saldo + dinero) < saldoMax) {
this.saldo = this.saldo + dinero;
System.out.println("Name: " + nombre + " deposit cash: " + dinero + " TOTAL CASH: " + getSaldo());
notifyAll();
} else {
System.out.println(" **** The account can't have more than 500 Euros. " + nombre + " tryed to deposit: " + dinero + " Cash now: " + getSaldo() + " ****");
System.out.println(" ---- " + nombre + " IS WAITING TO DEPOSIT MORE MONEY ---- ");
try {
wait();
} catch (Exception e) {
System.out.println(" CATCH WAIT PONER");
}
}
}// END METHOD PONER
public int getSaldo() {
return saldo;
}
}// END CLASS CUENTA
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.