I'm currently trying to modify a sequential prime program I worked on and incorporate multithreading. I'd like to specify the amount of threads to split the work, and then have each thread run an algorithm to find primes, then display how many primes are in each thread.
The problem seems to be that each thread is running the algorithm on the entire array, it's not splitting the array in the desired threads I specify. I experimented with some other things, but they pretty much just caused more errors.
As of right now, I'm experimenting on an array of 1000 numbers. Once I get it working, I would put millions of numbers in the array, but I just wanted to do less numbers first. There should be 168 prime numbers in the array of 1000. The problem is each thread is returning 168 primes, which is leading me to believe each thread is doing the same thing, not splitting up the work. My question is, how can I take this program, and get it to split the array and run the algorithm that way? So 0 to 499 would run the algorithm and display the amount of primes withing thread 0, then 500 to 1000 would run the alg and display how many primes are within that thread.
Here's my code - Prime.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Prime extends Thread {
Scanner scan = new Scanner(System.in);
static long [] primes = new long [1000];
static ArrayList<Integer> primeList = new ArrayList<Integer>();
//int max = num;
public int count = 0;
public void run() {
for (int n = 2; n<=primes.length; n++) {
boolean prime = true;
for (int j = 2; j < n; j++) {
if (n%j == 0){
prime = false;
break;
}
}
if (prime){
count++;
primeList.add(n);
}
}
}
}
Worker.java
import java.util.ArrayList;
import java.util.Scanner;
public class Worker {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How Many threads? ");
int nThreads = scan.nextInt(); // Create variable 'n' to handle whatever integer the user specifies. nextInt() is used for the scanner to expect and Int.
final Prime[] pThreads = new Prime[nThreads];
long startTime = System.currentTimeMillis();
for(int i = 0; i<nThreads; i++){
pThreads[i] = new Prime();
pThreads[i].start();
}
try {
for (int i = 0; i < nThreads; i++)
pThreads[i].join();
} catch (InterruptedException e) {
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Execution time = : "+ elapsedTime);
System.out.println("----------------------------------------------------");
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("How many Cores this Java Program used: " + cores);
for (int i = 0; i < nThreads; i++)
System.out.println("Thread " + i + " Prime count: " + pThreads[i].count); // Display Thread count
System.out.println("Total prime count: " + Prime.primeList.size()); // Output total amount of primes from the Array List
for (int i = 0; i < 100; i++) // Display first 100 primes.
System.out.println(Prime.primeList);
}
}
Any help would be appreciated. Thanks.