Question: General Instructions: This program is composed of 2 Java programs i have the first program and i want to make changes so that it fulfills

General Instructions: This program is composed of 2 Java programs i have the first program and i want to make changes so that it fulfills the 2nd part

this is the 1st part This program is a Java programs you will write a Thread class (can be a Thread subclass or Runnableinterface 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 thethreads. 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

the code is

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;

//call getByte method to get the range to process

//if there is no byte available-exit the thread execution

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();

//divide the range into equal bytes. Pass one byte to each thread for execution

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;

}

}

}

know this are the changes i want to make

IN this program The threads will call a method called getWork. This method will return an interval over which to count the number of primes. We refer to this bit of work as a bite. The interval should be small enough to allow for good load balancing, but large enough so that threads are not calling the getWork method constantly. The driver should be responsible for reporting the amount of time it takes to complete the whole job, and the total count of primes over the entire work range.

The output of the Program should report much more information (see next page for details)

1. Number of threads

2. Number of primes computed by each thread

3. Time used by each thread

4. Total number of primes

5. Total time used by the driver

6. Number of bites processed by each thread

7. Speedup

In my program, the following sample output is displayed.

I ran the program of an 8-core machine with a range of 3-10,000,000 with 8 threads.

THREAD-1 getting a bite

THREAD-8 getting a bite

THREAD-6 getting a bite

THREAD-4 getting a bite

THREAD-7 getting a bite

. . .

. . .

THREAD-5 getting a bite

THREAD-2 getting a bite

THREAD-3 getting a bite

THREAD-1 getting a bite

THREAD-6 getting a bite

THREAD-4 getting a bite

THREAD-7 getting a bite

The Program Results

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

THREAD-1: PRIMECOUNT: 91121 TIMER: 1.719127 sec TOTAL BITES: 54

THREAD-2: PRIMECOUNT: 73605 TIMER: 1.713799 sec TOTAL BITES: 45

THREAD-3: PRIMECOUNT: 83974 TIMER: 1.714943 sec TOTAL BITES: 51

THREAD-4: PRIMECOUNT: 97170 TIMER: 1.721303 sec TOTAL BITES: 58

THREAD-5: PRIMECOUNT: 74658 TIMER: 1.688700 sec TOTAL BITES: 45

THREAD-6: PRIMECOUNT: 84127 TIMER: 1.719229 sec TOTAL BITES: 51

THREAD-7: PRIMECOUNT: 78419 TIMER: 1.722470 sec TOTAL BITES: 47

THREAD-8: PRIMECOUNT: 81504 TIMER: 1.678458 sec TOTAL BITES: 49

RANGE: 10,000,000 BITE SIZE = 25,000

TOTAL PRIMES: 664,578

TOTAL THREAD TIME: 13.678028 sec

RUNNING TIME: 1.760415 sec

SPEEDUP: 7.77

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!