The thing is that the execution of the threads of my program stops when it reaches the wait (), having a notify () after ...
package supermercado;
import static java.lang.Thread.sleep;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Supermercado {
static float ingresos;
static ArrayList<Cliente> arrClientes = new ArrayList<Cliente>();
static ArrayList<Caja> arrCajas = new ArrayList<Caja>();
static ArrayList<Thread> arrTClientes = new ArrayList<Thread>();
static ArrayList<Thread> arrTCajas = new ArrayList<Thread>();
public Supermercado() {
Scanner sc;
int n1 = 0, n2 = 0;
do {
try {
sc = new Scanner(System.in);
System.out.println("Introduce cantidad de clientes:");
n1 = sc.nextInt();
} catch (Exception ex) {
System.out.println("Formato incorrecto");
n1 = 0;
}
} while (n1 <= 0);
do {
try {
sc = new Scanner(System.in);
System.out.println("Introduce cantidad de cajas:");
n2 = sc.nextInt();
} catch (Exception ex) {
System.out.println("Formato incorrecto");
n2 = 0;
}
} while (n2 <= 0);
for (int i = 0; i < n1; i++) {
Cliente c = new Cliente();
arrClientes.add(c);
arrTClientes.add(new Thread(c));
}
for (int i = 0; i < n2; i++) {
Caja c = new Caja();
arrCajas.add(c);
arrTCajas.add(new Thread(c));
}
for (int i = 0; i < arrTClientes.size(); i++) {
arrTClientes.get(i).start();
}
try {
sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(Supermercado.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < arrTCajas.size(); i++) {
synchronized (arrCajas) {
//arrCajas.get(i).notifyAll();
}
}
}
public static void main(String[] args) {
Supermercado sm = new Supermercado();
}
static synchronized void sumar(float n) {
ingresos += n;
}
public static class Caja extends Thread {
int ms;
public Caja() {
this.ms = (int) (Math.random() * 1500 + 1000);
}
@Override
public void run() {
System.out.println("Se están pasando los productos...");
try {
sleep(ms);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static class Cliente extends Thread {
int tiempo;
float pago;
public Cliente() {
this.tiempo = (int) (Math.random() * 1000 + 500);
pago = (float) (Math.random() * 50);
}
@Override
public void run() {
int nCaja = (int) (Math.random() * arrCajas.size());
synchronized (arrCajas.get(nCaja)) {
System.out.println("El cliente " + this.getName() + " va a comprar.");
try {
sleep(tiempo);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
arrCajas.get(nCaja).wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("El cliente " + this.getName() + " va a caja.");
arrTCajas.get(nCaja).start();
try {
arrTCajas.get(nCaja).join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("El cliente " + this.getName() + " va a pagar " + pago);
sumar(pago);
System.out.println("La recaudación actual total es de " + ingresos);
arrCajas.get(nCaja).notify();
}
}
}
}