I have a problem with the synchronization of threads in java, I comment.
I am trying to make a program such that I have an array of threads and I execute them all at the same time and whose only function is to move the position of a JLabel as if it were a race, the problem is that I have to launch all the threads and I have to do that until one of them has not taken a step do not move the next and that is to say that if I have 5 threads because the thread 2 takes a step and until it does not finish taking the step does not start step of another and so on until it reaches the goal but I can not get it to work well, I show you the code:
CONTROLLER CLASS: IT IS EVERYTHING I EXECUTE AND THAT CONTAINS MY ARRAY OF THREADS IN THE WINDOW CLASS
public class Controlador extends Thread {
@Override
public void run() {
//Ficha es un JLabel que contiene la imagen del corredor
//Ventana es el JFrame sobre el que se encuentran los JLabel
int delay = generarNumeroAleatorio();
while (!ventana.haLlegado(ficha)) {
//ventana.haLlegado devuelve true o false si ha llegado al final de la pantalla
ventana.movimientoSincronizado(this);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
ventana.mover(ficha);
});
}
}
WINDOW CLASS: CONTAINS THE COMPONENT AND EXTENDS FROM JFRAME
public class Ventana extends JFrame implements ActionListener {
private boolean poderMoverse;
private Controlador[] hilos = new Controlador[8];
public void mover(JLabel ficha)
{
ficha.setLocation(ficha.getX() + 50 , ficha.getY());
}
public synchronized boolean movimientoSincronizado(Controlador corredor)
{
while (!poderMoverse) //Mientras no pueda moverse dejo el hilo en espera
{
try {
//Cada hilo tiene como nombre la posicion que ocupa dentro del array
int id = Integer.valueOf(corredor.getName());
hilos[id].wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
poderMoverse = true; //Cambio el valor para que pueda moverse
notifyAll();
return poderMoverse;
}
I hope you can help me, Thank you very much everyone