Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is a Java programs you will write a Thread class (can be a Thread s

ID: 3589301 • Letter: T

Question

This program is a Java programs you will write a Thread class (can be a Thread subclass or Runnable interface implementer) which will be responsible in fining the number of prime numbers in a given range.

Program will require you to write a class which will receive a range of value and it will count the number of primes in that range. It should also return the amount of time it took to calculate the count. Time can be measured as the difference to two calls to System.nanoTime static method. The exact number of threads in your program should be variable between one to many. The number of threads can be a constant parameter t in your program. The program should then be able to allocate the proper ranges to the threads. The range for prime calculation should be from 3 to n. If n = 5,000,000 and t = 2, then thread 1 gets [3 - 2,500,000] and thread 2 gets [2,500,001 -5,000,000]. If n = 5,000,000 and t = 3, then thread 1 gets [3-1,666,666] and thread 2 gets [1,666,667-3,333,333] and thread3 gets [3,333,334-5,000,000], etc.

Specific Instructions:

A sample output of Program 1is shown below.

----------run----------

# Threads = 1

Thread #1 Prime Count = 348512

# Seconds Used = 9.795684926

Total Prime Count = 348512

----------run----------

# Threads = 2

Thread #1 Prime Count = 183071

# Seconds Used = 7.62562728

Thread #2 Prime Count = 165441

# Seconds Used = 9.353472019

Total Prime Count = 348512

Explanation / Answer

Please find below code and execute it.........

PrimeThreads.java

import java.util.Scanner;

class PrimeNumber extends Thread {

private Thread t;

private String threadName;

private int startRange;

private int endRange;

private int count=0;

private static int countAll=0;

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

PrimeNumber( String name,int startRange, int endRange ) {

threadName = name;

this.endRange=endRange;

this.startRange=startRange;

System.out.println("#"+ threadName);

}

public void run() {

long startTime = System.nanoTime();

  

  

//System.out.println("Running " + threadName );

try {

int flag=0;

for(int i = startRange; i <= endRange; i++)

{

for(int j = 2; j < i; j++)

{

if(i % j == 0)

{

flag = 0;

break;

}

else

{

flag = 1;

}

}

if(flag == 1)

{

//System.out.println(i);

count++;

}

}

System.out.println(threadName+" Prime Count::"+count);

System.out.println(threadName+" # Seconds Used " + (System.nanoTime() - startTime) );

countAll+=count;

System.out.println("Total Count:"+countAll);

}catch (Exception e) {

System.out.println(e.getMessage());

}

}

public void start () {

//System.out.println("Starting " + threadName );

if (t == null) {

t = new Thread (this, threadName);

t.start ();

}

}

}

public class PrimeThreads {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println ("Enter maximum Range of prime numbers:");

int range = sc.nextInt();

int t=0;

while(range>0){

System.out.println ("Enter t Value:");

t = sc.nextInt();

if(t!=0)

break;

}

int midval=range/t;

int start=3;

int endval=midval;

for(int m=1;m<=t;m++){

PrimeNumber T1 = new PrimeNumber( "Thread-"+m,start,endval);

T1.start();

//1System.out.println(T1.getCount());

start=midval;

endval+=midval;

}

  

}

}

-----------------------------

Thank you.. All the best

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote