modify behavior of the last thread of a group

0

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: (

    
asked by Montse Mkd 13.03.2018 в 13:28
source

1 answer

1

You do not control who passes the concurrent resource to whom, all the threads compete for that resource and eventually all will end up occupying it in sequence. But this sequence is indeterminate.

Therefore, you can not arbitrarily tell a thread not to "pass" the testimonial to another.

One way to solve the problem would be to have an accountant in the Testimoni class of how many times they have released the resource and we can take advantage of the method agafa() to return this amount to whoever takes the resource:

public class Testimoni  {
   /*
    * Modifiqueu i afegiu el codi necessari per implementar la classe 
    * Comandament.
    */
    boolean avalible = true;
    int vecesLiberado = 0;

    synchronized int agafa() {
        while (avalible == false) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }

        avalible = false;
        notify();
        return vecesLiberado;
    }

    synchronized void deixa() {
        avalible = true;
        ++vecesLiberado;
        notify();
    }
}

Then each corridor when invoking:

int veces = comandament.agafa();

You can determine if the number of times has been met to pass the Testimoni to another runner.

    
answered by 13.03.2018 / 13:46
source