3-wire sum

1

I hope you can help me, I have this thread problem ... Create a program that evaluates

  

p = Σfrom i = -100 to 100 (sin (i)) + Σfrom i = -100 to 100   of (cosine (i)) + Σfrom i = -100 to 100 of (tangent (i))

For each term in the equation, use a thread and at the end perform the addition.

I have this, this is the sum 1 of the breast ...

public class Suma1 extends Thread{
    public Suma1(String msg){
        super(msg);
    }

    @Override
    public void run(){
        int i;
        double s=0;
        for(i=-100;i<=100;i++){
            s+=(s+Math.sin(i));
        }
        System.out.println("Hilo 1");
        System.out.println(s);
    }
}

For the sum 2 of the cosine is this ...

public class Suma2 extends Thread{
    public Suma2(String msg){
        super(msg);
    }

    @Override
    public void run(){
        int i;
        double s=0;
        for(i=-100;i<=100;i++){
            s+=(s+Math.cos(i));
        }
        System.out.println("Hilo 2");
        System.out.println(s);
    }
}

and for the sum 3 of the tangent, this is ...

public class Suma3 extends Thread{
    public Suma3(String msg){
        super(msg);
    }

    @Override
    public void run(){
        int i;
        double s=0;
        for(i=-100;i<=100;i++){
            s+=(s+Math.tan(i));
        }
        System.out.println("Hilo 3");
        System.out.println(s);
    }
}

This is my main ...

public class HiloNum7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Thread hilo1=new Suma1("Proceso numero 1");
        Thread hilo2=new Suma2("Proceso numero 2");
        Thread hilo3=new Suma3("Proceso numero 3");
        Thread hilo4=new SumaTotal("Proceso numero 4");

        hilo1.start();
        hilo2.start();
        hilo3.start();
        hilo4.start();
    }

}

What I can not do is the general sum, I hope you can help me, my idea was to create another thread to add the 3 threads, but I have not the slightest idea how to do it: (

    
asked by Paloma Martínez 26.10.2018 в 00:46
source

1 answer

3

You already have a thread in the thread main. And to add three numbers you do not need an independent thread .

What you have to do is that the main thread does not finish its execution before having the data of the three threads , and find a way to recover the result of every thread .

For the first thing, the most direct thing is to invoke the join() method of each thread . This will block the thread from where it is invoked until the invoked thread has finished its execution.

public static void main(String[] args) {
   ...
   hilo1.join();
   hilo2.join();
   hilo3.join();
   // Aquí sabes que los tres threads han terminado. Puedes empezar el cálculo final.
}

Another option is to make a loop that checks the value of Thread.isAlive() until the three return false . But better the one above.

To obtain the value of each thread, for example by adding an attribute to each thread and saving the result there; Bonus points if you access through a method get

public class Suma1 extends Thread {

   private double resultado; // Aquí guardas el resultado.

   public double getResultado() {
     return this.resultado;
   }
   ...

and in your main you only do hilo1.getResultado() + hilo2.getResultado()... .

Apart from this, for this type of work java.util.concurrent.Future fits better.

    
answered by 26.10.2018 в 01:00