I am trying to populate a static array using multiple threads. The suggested usage of join()
in https://stackoverflow.com/a/1492077/10418317 seems to be imposing a specific order on the threads. I want them to run in parallel if they can so as to speed up the whole process
Assigment
public class Assigment {
public static int minPrime;
public static int maxPrime;
public static int[] masterMin = new int[10];
public static int[] masterMax = new int[10];
public static void main(String[] args) {
// TODO code application logic here
for(int i = 1; i < 11;i++){
Thread thread = new Thread(new NumGenerator(i));
thread.start();
System.out.println("Thread " + i + " generating nums");
}
Arrays.sort(masterMin);
Arrays.sort(masterMax);
minPrime = masterMin[0];
maxPrime = masterMax[9];
System.out.println("<------------------------------>");
System.out.println("Max Prime is " + maxPrime);
System.out.println("Min Prime is " + minPrime);
}
}
NumGenerator
public class NumGenerator implements Runnable {
int[] numbers;
int thrnum;
public NumGenerator(int thrnum){
this.thrnum = thrnum;
}
@Override
public void run(){
Random rand = new Random();
numbers = new int[3000];
for(int i = 0; i<3000; i++){
numbers[i] = rand.nextInt(899999)+1;
}
Searcher searcher = new Searcher(numbers);
Assigment.masterMax[thrnum-1] = searcher.max;
Assigment.masterMin[thrnum-1] = searcher.min;
}
I am trying to sort the array after every thread has completed inputting into it and then print out the first and the last i.e the smallest and the largest values.The problem is that the arrays masterMin
and masterMax
seem to be still having all zero entries after the supposed population through Assigment.masterMin[thrnum-1] = searcher.min;
Searcher is another class which is actually giving me non-zero values for search.max
and search.max
as expected.