The idea for this program is to set up a runnable object to
The idea for this program is to set up a runnable object to find the nth prime number. The main function will repeatedly ask the user to enter a number which indicates which prime number the user wants to find. The main program will create a runnable object with the user input. The runnable object is given to a thread pool for execution. A thread will execute the runnable object and eventually find and output the answer.
Your program should be setup to permit up to 3 threads running at the same time. The easiest way to accomplish this is by setting up a thread pool. A user input of 0 means that the user is finished entering values. The main program must wait until all threads complete before exiting.
The following function finds the nth prime number. You should be able to use this in your program to do the calculations.
void findnthprime(int nthprime)
{
int prime, //keeps track of when a prime # is found.
number = 0; //represents integers starting at 3.
boolean isPrime = true;
if (nthPrime <= 0) {
System.out.printf(\"Bad input – must be positive number\ \");
return;
}
if (nthPrime == 1) {
System.out.printf(\"Prime #%d = %d\ \", 1, 2);
} else {
// otherwise use this loop which goes through each number
// and checks if it is prime until \"prime\" is equal to
// \"nthprime\".
prime = 1;
number = 2;
while (prime < nthPrime) {
number++;
isPrime = true;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime == true) {
prime++;
}
}
System.out.printf(\"Prime #%d = %d\ \", prime, number);
}
}
To test your program, you will need to input rather large values to actually get multiple threads going simultaneously. Try inputting numbers between 12000 and 30000. This should take a while for the program to calculate.
Solution
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.*;
public class IsPrime
{
public static void main(String[] args)
{
MyFun();
}
private static void MyFun()
{
class PrimeNumberGen implements Runnable
{
int nthPrime = 0;
PrimeNumberGen(int n) {nthPrime = n;}
void findnthprime()
{
int prime, //keeps track of when a prime # is found.
number = 0; //represents integers starting at 3.
boolean isPrime = true;
if (nthPrime <= 0)
{
System.out.printf(\"Bad input – must be positive number\ \");
return;
}
if (nthPrime == 1)
{
System.out.printf(\"Prime #%d = %d\ \", 1, 2);
}
else
{
// otherwise use this loop which goes through each number
// and checks if it is prime until \"prime\" is equal to \"nthprime\".
prime = 1;
number = 2;
while (prime < nthPrime)
{
number++;
isPrime = true;
for (int i = 2; i <= number / 2; i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime == true)
{
prime++;
}
}
//Displays the thread id, prime number and number
System.out.printf(\"Thread #: %s Prime #%d = %d\ \", Thread.currentThread().getId(), prime, number);
}
}
//Overloads the Runnable interface run method
public void run()
{
findnthprime();
}
}
Scanner sc = new Scanner(System.in);
System.out.println(\"\ Enter a number: \");
int num = sc.nextInt();
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 2;i <= num; i++)
{
Runnable worker = new PrimeNumberGen(i);
executor.execute(worker);
}
}
}
Output:
Enter a number:
10
Thread #: 10 Prime #2 = 3
Thread #: 11 Prime #3 = 5
Thread #: 12 Prime #4 = 7
Thread #: 11 Prime #6 = 13
Thread #: 10 Prime #5 = 11
Thread #: 11 Prime #8 = 19
Thread #: 12 Prime #7 = 17
Thread #: 11 Prime #10 = 29
Thread #: 10 Prime #9 = 23


