javamultithreading

Multi-Threaded Prime number Finder in Java


I am making a java program that I have to find all prime numbers and the count of the prime numbers up to 200 million. I have to use trial division with static global variable that all the threads share to hold the next number to be checked if prime. As it finds a prime it adds it to a array then displays the array upon completion. here is what I have so far and all my threads are showing the same number of primes found as the total number of primes can anyone help with this.

Main-

//*import java.util.Scanner;
    public class MultiThreadedPrimeFinder {
    static final int nThreads = 2;

    public static void main(String[] args) throws InterruptedException{
        int t;
        int total = 0;
        PrimeThread[] pthreads = new PrimeThread[nThreads];
        //*Scanner kb = new Scanner(System.in);
        //*System.out.println("Enter a Positive Integer: ");
        //*long num = kb.nextLong();
        long starttime, endtime, runtime, a = 0;
        starttime = System.currentTimeMillis();
        for(int i = 0; i <10000000; i ++)
            a+=i;
        for (t=0; t<nThreads; t++)
        {
            pthreads[t] = new PrimeThread();
            pthreads[t].start();
        }

        for (t=0; t<nThreads; t++)
        {
            pthreads[t].join();
            System.out.println("Thread "+t
                    +"  Prime count: "+ pthreads[t].count);
        }
        total = PrimeThread.count;
        System.out.println("Total prime count: "+total);
        for (int i=0;i<100; i++)
            System.out.println(""+i+": "+PrimeThread.primes[i]);
        endtime = System.currentTimeMillis();
        runtime = endtime - starttime;
        System.out.println("The run time is " +runtime +" milliseconds");

    }

    }

Class -

public class PrimeThread extends Thread{
static long nextNumber=3;
static final long max = 1000;
public static int count=0;
public long thread = 100;
public static long[] primes = new long[100000]; 


public void run() {
    long myNumber;
    while ((myNumber=getNextNumber())<=max) {
        primes[0] = 2;
        if (prime(myNumber)) {

                primes[count++] = myNumber;
            }
        }
    }


public static synchronized long getNextNumber() {
    long n = nextNumber;
    nextNumber +=2;
    return n;
}

public boolean prime(long n) {
    int i;

    for (i=3; i * i<=n; i+=2)
        if (n%i==0) return false;
    return true;
}
}

the output looks like this

Thread 0  Prime count: 167
Thread 1  Prime count: 167
Total prime count: 167
0: 2
1: 5
2: 7
3: 11
4: 13
5: 17
6: 19
7: 23
8: 29
9: 31
10: 37
11: 41
12: 43
13: 47
14: 53
15: 59
16: 61
17: 67
18: 71
19: 73
20: 79
21: 83
22: 89
23: 97
24: 101
25: 103
26: 107
27: 109
28: 113
29: 127
30: 131
31: 137
32: 139
33: 149
34: 151
35: 157
36: 163
37: 167
38: 173
39: 179
40: 181
41: 191
42: 193
43: 197
44: 199
45: 211
46: 223
47: 227
48: 229
49: 233
50: 239
51: 241
52: 251
53: 257
54: 263
55: 269
56: 271
57: 277
58: 281
59: 283
60: 293
61: 307
62: 311
63: 313
64: 317
65: 331
66: 337
67: 347
68: 349
69: 353
70: 359
71: 367
72: 373
73: 379
74: 383
75: 389
76: 397
77: 401
78: 409
79: 419
80: 421
81: 431
82: 433
83: 439
84: 443
85: 449
86: 457
87: 461
88: 463
89: 467
90: 479
91: 487
92: 491
93: 499
94: 503
95: 509
96: 521
97: 523
98: 541
99: 547
The run time is 17 milliseconds

Solution

  • You have

    public static int count=0;
    

    which keeps track of the number of primes gotten in total. Since it's static, pthreads[0].count == pthreads[1].count == PrimeThread.count. To see the primes retrieved by the individual threads, add an instance counter:

    public int myCount = 0;
    ....
    primes[count++] = myNumber;
    myCount++;
    ...
    System.out.println("Thread "+t
            +"  Prime count: "+ pthreads[t].myCount);
    

    Also, to prevent interleaving of count++, you should synchronize when incrementing it.