0

been working on this program for a while and I think I've made much more progress. My java skills are not very good, but I think I'm close. Everything should compile without issue except for my "public void run" in my worker class. The program prompts the user for how many threads they want and then parses through a text file of random numbers to find all the prime numbers. My issue seems to be in the algorithm for the prime numbers. How do I write the algorithm so it parses the data down and finds the prime numbers?

I have posted the entire program below, but please see the worker class towards the bottom. Any help would be greatly appreciated in solving this issue. Thank you.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class PrimeNumbers{

    public static void main(String[] args) throws IOException {
        int[] numbers = new int[100000];
        int count;
        int index = 0;
        String datafile = "dataset529.txt"; //string which contains datafile
        String line; //current line of text file



        try (BufferedReader br = new BufferedReader(new FileReader(datafile))) { //reads in the datafile
            while ((line = br.readLine()) != null) { //reads through each line
                numbers[index++] = Integer.parseInt(line); //pulls out the number of each line and puts it in numberset[]
            }
        }

        System.out.println("How many threads would you like to use?");
        Scanner scan = new Scanner(System.in);
        int z = scan.nextInt();


        Thread[] threads = new Thread[z]; //creates threads as per user
        worker[] finder = new worker[z]; //assigns finder to each thread created


        int range = numbers.length / z; //breaks up each worker into a section depending on thread count.
        for (count = 0; count < z; count++) {
            int startAt = count * range;
            int endAt = startAt + range;
            finder[count] = new worker(startAt, endAt, numbers);

        }

        for (count = 0; count < z; count++) {           //moves to next thread
            threads[count] = new Thread(finder[count]);
            threads[count].start();
        }

        boolean processing = false;
        do {
            processing = false;
            for (Thread t : threads) {
                if (t.isAlive()) {
                    processing = true;
                    break;
                }
            }
        } while (processing);

        for (worker worker : finder) {
            System.out.println("Max of thread is: " + worker.getPrime());
        }



    }

    public static class worker implements Runnable {

        private int start;      
        private int stop;        
        private int numberset[];


        public worker(int start, int stop, int[] numberset) {
            this.start = start;
            this.stop = stop;
            this.numberset = numberset;
        }

        @Override
        public void run() {
            for (int x = start; x < stop; x++) {  
                if (numberset[]%3 && != 1 && != 2 && !=3)
                    return prime

            }
        }

        public int getPrime() {
            return true
        }
    }
}
  • what is `if (numberset[]%3 && != 1 && != 2 && !=3)` meant to mean? I think you can remove the entire sourcecode and concentrate on that. Ps: Threads don't return a value. See `Callable` instead of `Runnable`. – zapl Dec 03 '15 at 22:30
  • That is where I want to find the prime. I'm a little lost lol. I used this program originally to find max values using threads and that worked fine. I – Chris Jones Dec 03 '15 at 22:33
  • you should start with what approach/algorithm you want to use for prime finding and then start coding ... (not all are suitable for multi-threading) for example see [Prime numbers by Eratosthenes quicker sequential than concurrently?](http://stackoverflow.com/a/22477240/2521214) my answer is in C++ (code is non parallel) and have a lot of performance hints to offer the Question itself is in JAVA – Spektre Dec 04 '15 at 08:45

0 Answers0