1
I’m starting to learn parallel programming, and I’m looking to compare a program with a single thread with a multi thread.
What I thought was to make a very simple algorithm that, in a 1-minute interval, would calculate as many prime numbers as possible, and, show me the last calculated prime number and its position in prime numbers, for example, let’s say it was number 23, would appear number 23 and its position, in case 9, because it is the 9th prime number.
Without using parallelism, the number found was 774107, at position 62039. However, using parallelism, I obtained the number 1083757, at position 84444 (the wrong position, the right one would be 84547), I believe it was a very basic mistake, but, as I still do not understand much about parallelism, I could not solve it.
Below follows the code of the two classes I created, the first, is the Calculates class that only serves to define the instances and implement the run method. The second is Main class.
Calculates:
import java.util.Collection;
public class Calcula extends Thread {
private int x;
private int quantidade = 0;
private int ultimo = 0;
private long tempoInicial;
public Calcula(int x, long tempoInicial) {
this.x = x;
this.tempoInicial = tempoInicial;
}
public int getQuantidade (){
return quantidade;
}
public int getUltimo (){
return ultimo;
}
@Override
public void run() {
long tempoMaximo=tempoInicial;
while (tempoMaximo < tempoInicial+60000){
tempoMaximo = System.currentTimeMillis();
for(int z=2; z<x/2; z++){
if (x%z==0) break;
else if(z==(x/2)-1){
quantidade++;
ultimo=x;
}
}
x=x+8;
}
}
}
Leading:
import java.util.ArrayList;
import java.util.Collection;
public class Principal{
public static void main(String[] args){
long tempoInicial = System.currentTimeMillis();
Calcula t1 = new Calcula (5, tempoInicial);
Calcula t2 = new Calcula (7, tempoInicial);
Calcula t3 = new Calcula (9, tempoInicial);
Calcula t4 = new Calcula (11, tempoInicial);
t1.start();
t2.start();
t3.start();
t4.start();
try{
t1.join();
t2.join();
t3.join();
t4.join();
} catch (InterruptedException ex){
ex.printStackTrace();
}
int ultimo = t1.getUltimo();
if (ultimo < t2.getUltimo()) ultimo = t2.getUltimo();
if (ultimo < t3.getUltimo()) ultimo = t3.getUltimo();
if (ultimo < t4.getUltimo()) ultimo = t4.getUltimo();
System.out.println("Último primo encontrado: " + ultimo);
System.out.println("Quantidade de primos encontrados: " + (t1.getQuantidade() + t2.getQuantidade() + t3.getQuantidade() + t4.getQuantidade()));
}
}
The logic I followed was to start each thread with a different value and implement them 8 by 8, so that no repeat values are calculated. In the end, I take the getQuatity of each and each, and I see the greatest getUltimo to get the highest value. I believe the error is because some thread is calculating a little faster, then the amount goes wrong.