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

i want the program in JAVA General Instructions: In This program you will write

ID: 3591681 • Letter: I

Question

i want the program in JAVA

General Instructions: In This program 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.

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

Explanation / Answer

import com.github.threadcontext.ContextManager;
import com.github.threadcontext.PropagatingExecutorService;
import com.github.threadcontext.ThreadLocalSaver;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

final class Main {

    // Step 1: instrument thread pool
    private static final ExecutorService executorService = new PropagatingExectorService(Executors.newFixedThreadPool(8));

    // Step 2: register ThreadLocal
    private static final ThreadLocalSaver example = new ThreadLocal<String>();
    static {
        ThreadContext.savers.add(new ThreadLocalSaver<>(example));
    }

    private static void countdown(int n) {
        if (n > 0) {
            executorService.submit(() -> {
                System.out.println(String.format("%d %s", n, example.get()));
                work(n - 1);
            });
        }
    }

    public void main(String[] args) {
        example.set("example1");
        countdown(10); // these tasks print example1
        example.set("example2");
        countdown(20); // these tasks print example
    }
}