I have the following program:
public class Corredor extends Thread {
Testimoni comandament;
static int canal;
public Corredor(Testimoni c) {
comandament = c;
canal = 0;
}
@Override
public void run() {
//Agafa el comandament
comandament.agafa();
System.out.println(getName() + " esta corren");
//Mira la tele
try {
Thread.sleep((long) (Math.random() * 350) + 300); //utilitzem el mètode sleep per suspendre l'execució del Thread un temps aleatori de ms
} catch (InterruptedException e) {
System.out.println("Error!!!");
}
System.out.println("Pasa el testimoni al seguent corredor:");
//Deixa el comandament
comandament.deixa();
}
}
public class Relleus {
public static void main(String[] args) throws InterruptedException {
Testimoni c = new Testimoni();
int numMembres = 5; //la familia pot ser més gran o més petita
//Es creen el membres de la família, se'ls dona un nom i es posen a mirar la tele
Corredor[] membre = new Corredor[numMembres];
for(int i=1; i<numMembres; i++) {
membre[i] = new Corredor(c);
membre[i].setName("CORREDOR : "+i);
membre[i].start();
}
//Esperem que tots acabin de mirar la tele
for(int i=1; i<numMembres; i++) {
membre[i].join();
}
System.out.println("Carrera acabada!!");
}
}
public class Testimoni {
/*
* Modifiqueu i afegiu el codi necessari per implementar la classe
* Comandament.
*/
boolean avalible = true;
synchronized void agafa() {
while (avalible == false) {
try {
wait();
} catch (InterruptedException e) {}
}
avalible = false;
notify();
}
synchronized void deixa() {
avalible = true;
notify();
}
}
I need to do an if.
That if runner 4 is running, do not pass the testimonial. Now it is done in such a way that testimony 1 passes it to 2, 2 to 3 and 3 to 4 .. but I need 4 to not do it.
In the RELLEUS class it is where I create the runners. As I can do for the CORRIDOR class, I could use for example the membre [5] to be able to do an if inside the RUN () method ??? something similar to: if runner == membre [1], membre [2], membre [3] the testimony passes. else the testimony does not pass.
The problem is that I do not know how I can take that data or extract that information ...
Thanks: (