Prime Number Program In Java

Started by
1 comment, last by frob 1 year, 2 months ago

The main purpose of this software is for one thread to compute one-based prime numbers. The number of threads will be determined by what the Java JVM reports back to the int cores variable within the code.

Claimed Lock: 0; Claimed Number: 75825; isPrime: false
Claimed Lock: 1; Claimed Number: 75828; isPrime: false
Claimed Lock: 2; Claimed Number: 75831; isPrime: false
Claimed Lock: 4; Claimed Number: 75835; isPrime: false
Claimed Lock: 5; Claimed Number: 75837; isPrime: false
Claimed Lock: 5; Claimed Number: 75837; isPrime: false
Claimed Lock: 3; Claimed Number: 75833; isPrime: true
Claimed Lock: 6; Claimed Number: 75839; isPrime: false.

(You can see that two locks, i.e. 5, are called twice - they should be distinguishable)

Here's a basic rundown of my programme:

The software begins by determining the number of logical CPU cores on the specified machine.

Creates a two-dimensional array of [2] [Number of Processor Cores].

Array[0][i] = Contains 2k+1 (odd numbers) possible prime numbers (doing this since 2 is the only known prime number that is even)

Array[1][i] = Fills with "-1" = Indicates that the number is ready to be picked up by a process/thread.

The software then starts up n threads (based on cpu core count)

A newly formed thread is expected to seek for the next available Array[1][i] == -1 and update its value to 2. (2 denotes a process lock and/or the integer is being tested to see if it is prime or not).

-6a. The n child processes verify if the value is prime and modify the Array[1][i] to either 1 or 0. (1 meaning it is prime or 0 meaning its not prime

-6b. Child Procedure Completes

Main, the parent process, will wait until all Array[1][i] are either 1 or 0.

Return to step 3.

I'm working on a multi-threaded prime number generator. My intention with this multi-threaded process is for each process to check a single integer. I have some functioning code, but I'm having trouble understanding this source here with the "locking" notion, in which the schedular is running two processes extremely near to one other.

What am I missing or doing incorrectly with this logic? With the JVM/OS Schedular, I suppose I am missing something. Yet I have a feeling I could be wrong about this as well. What could I do to solve this problem?

Here is my code: Class with Many Threads

class MultithreadCalculate extends Thread {
    public void run() {
        try {
            int indexNum = -1;
            for (int i = 0; i < MultiThreadPrimeNumGen.cores; i++) {
                if (MultiThreadPrimeNumGen.primeArray[1][i] == -1) {
                    MultiThreadPrimeNumGen.primeArray[1][i] = 2;
                    indexNum = i;
                    break;
                }
            }

            boolean isPrime = true;
            for (int i = 2; i < MultiThreadPrimeNumGen.primeArray[0][indexNum]; i++) {
                if (MultiThreadPrimeNumGen.primeArray[0][indexNum] % i == 0) {
                    isPrime = false;
                    MultiThreadPrimeNumGen.primeArray[1][indexNum] = 0;
                    break;
                }
            }

            if (isPrime) {
                MultiThreadPrimeNumGen.primeArray[1][indexNum] = 1;
            }

            System.out.println("Thread " + Thread.currentThread().getId() + "; Claimed Lock: " + indexNum + "; Claimed Number: " + MultiThreadPrimeNumGen.primeArray[0][indexNum] + "; isPrime: " + isPrime);
        }
        catch (Exception e) {
            System.out.println("Exception is caught");
        }
    }
}

Here is the main class:

public class MultiThreadPrimeNumGen {
    public static int[][] primeArray;
    public static int primeBase = 1;
    public static int cores;

    private static void fillArray() {
        for (int i = 0; i < cores; i++) {
            primeBase += 2;
            primeArray[0][i] = primeBase;
        }

        for (int i = 0; i < cores; i++) {
            primeArray[1][i] = -1;
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File(System.getProperty("user.home") + "/Desktop" + "/PrimeNumber.txt");
        PrintWriter out = new PrintWriter(file);

        //Gets number of CPU Cores
        cores = Runtime.getRuntime().availableProcessors();
        System.out.println("Number of Cores: " + cores);

        while (true) {
            primeArray = new int[2][cores];
            fillArray();
            for (int i = 0; i < cores; i++) {
                MultithreadCalculate multithreadCalculate = new MultithreadCalculate();
                multithreadCalculate.start();
            }

            while (true) {
                boolean flag = false;
                for (int i = 0; i < cores; i++) {
                    if ((primeArray[1][i] == 0) || (primeArray[1][i] == 1)) {
                        flag = true;
                    } else {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    break;
                }
            }
            for (int i = 0; i < cores; i++) {
                if (primeArray[1][i] == 1) {
                    out.println("PrimeNum: " + primeArray[0][i]);
                    out.flush();
                }
            }
        }
    }
}

Advertisement

Homework-related questions generally aren't allowed, although sometimes we do permit very specific questions about specific issues on what is clearly a homework problem.

Allowed: In my assignment, I'm struggling because this statement on line 184 looks like it should be calculating 37, but it's calculating 39. Can anyone help me?

Not allowed: In my assignment, I'm struggling because of an error in the overall logic. I'm not sure what I missed, can anyone help me?

That is, if you have identified the issue and are struggling with the technical details of programming it's generally okay, but if you haven't identified the overall problem that's where your learning needs to take place.

sasha_brouse said:
What am I missing or doing incorrectly with this logic? With the JVM/OS Schedular, I suppose I am missing something. Yet I have a feeling I could be wrong about this as well. What could I do to solve this problem?

This falls into the latter bucket. I can see a few things, but this is an issue better discussed with your teacher, both to help them teach you better, and to help you learn better.

This topic is closed to new replies.

Advertisement