Somatório Multithead

Asked

Viewed 382 times

0

I am trying to make a multithread sum but the value is very discrepant: the value that should be the sum of all numbers 499500 what is being the result: 1561250

Main Class:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Ex1 {

    public static void main(String[] args) throws InterruptedException{
        int soma = 0;
        int qtd = 250;
        int inicio = 0;
        int qtdThreads = 4;
        int qtdValores = 1000;
        int[] a = new int[qtdValores];
        for (int i = 0; i < qtdValores; i++){
            a[i] = i;
        }

         //cria pool de threads para execução de tarefas
        ThreadPoolExecutor p = new ThreadPoolExecutor(5, 10, 1, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(10));
        List<Soma> listThread = new ArrayList<Soma>();

        for(int i = 0; i < qtdThreads; i++){
            listThread.add(new Soma(a,inicio));
            p.submit(listThread.get(i));
            inicio += qtd;
        }

        //força a execução e finalização das threads
        p.shutdown();

        //aguarda finalização das threads em execução
        p.awaitTermination(1, TimeUnit.DAYS);

        for(int i = 0; i < qtdThreads; i++){
            soma += listThread.get(i).soma;
        }

        System.out.println(soma);
    }

}

Soma class:

public class Soma extends Thread{
    private int qtd = 250;
    public int inicio;
    public int soma = 0;
    public int[] a;

    public Soma(int[] a, int inicio){
        this.a = a;
        this.inicio = inicio;
    }

    public void run(){
        for(int i = inicio; i < i + qtd;i++){
            soma += a[i];
        }
    }
}
  • for(int i = inicio; i < i + qtd;i++){ Is that right? What should he do?

  • start the sum at a given start position (because several threads are summing up in dierents) and stop at the last position before the start position of the next thread add up

  • I still don’t understand your code, but you’re bound to get out of this for the variable qtd should be of 0 down, right? And I don’t see the variable qtd being modified nowhere after startup.

  • Qtd is not to be modified at any location, and is leaving the.

  • @Math this qtd is the amount of values of the vector to be read and summed.

1 answer

2


The problem is in the following code snippet:

public void run(){
    for(int i = inicio; i < i + qtd;i++){
        soma += a[i];
    }
}

You must do:

public void run(){
    for(int i = inicio; i < inicio + qtd;i++){
        soma += a[i];
    }
}

For you have assumed that it must go from the beginning to the beginning plus the amount, but when using i instead of inicio its value that should be the initial is varying with each iteration.

  • Puts, truth was beginner’s mistake.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.