Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using this JAVA Code: import java.util.concurrent.*; import java.math.BigInteger; public class Primes { private static Long nonprime = new Long(0); public static Callable countPrimes(final BigInteger start,

Using this JAVA Code:

import java.util.concurrent.*;

import java.math.BigInteger;

public class Primes

{

private static Long nonprime = new Long(0);

public static Callable countPrimes(final BigInteger start,

final long length) {

Callable callableObj = () -> {

BigInteger n = start;

for (long i = 0; i < length; i++)

{

if (n.isProbablePrime(100))

System.out.println(n);

nonprime++;

}

return Primes.nonprime;

};

return callableObj;

}

public static void main(String[] args)

{

Callable c1 = countPrimes(new BigInteger("1000000000000000"), 500);

Callable c2 = countPrimes(new BigInteger("1000000000500000"), 500);

ExecutorService service1 = Executors.newFixedThreadPool(2);

long start = System.currentTimeMillis();

long end = System.currentTimeMillis();

//Submit them both to the same ExecutorService as in the preceding example.Youll get two Future values.

Future f1 = service1.submit(c1);

try {

System.out.println(f1.get());

System.out.println(nonprime);

end = System.currentTimeMillis();

System.out.println("Milliseconds: " + (end - start));

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//Submit them both to the same ExecutorService as in the preceding example.Youll get two Future values.

Future f2 = service1.submit(c2);

start = System.currentTimeMillis();

try {

System.out.println(f2.get());

System.out.println(nonprime);

end = System.currentTimeMillis();

System.out.println("Milliseconds: " + (end - start));

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

service1.shutdown();

}

}

Increment a shared counter. Add a field:

private static long nonprime = 0;

In the countPrimes method, increment nonprime when a number isnt a prime. After printing each result, add a call

System.out.println(nonprime);

Create a method that places primes into a queue:

public Runnable producePrimes(BigInteger start, long length, BlockingQueue queue)

You will want to use put, not add for placing items into the queue so that thethread will wait if the queue is full. Add a method:

public Runnable consumePrimes(BlockingQueue queue);

Use a ReentrantLock. Make a Counter class with synchronized methods increment and get. Remove the lock from the previous step and make nonPrime into an instance of your Counter class

Create a method that places primes into a queue:

public Runnable producePrimes(BigInteger start, long length, BlockingQueue queue)

You will want to use put, not add for placing items into the queue so that the thread will wait if the queue is full. Add a method

public Runnable consumePrimes(BlockingQueue queue);

that removes primes from the queue and prints those that have at most three distinct digits. You will want to use the method take to remove items from the queue so that the thread will wait if there are no items to remove. Here is a method for getting all distinct characters in a string:

private static String distinct(String s)

{

StringBuilder result = new StringBuilder();

int i = 0;

while (i < s.length())

{

int cp = s.codePointAt(i);

int cc = Character.charCount(cp);

if (result.indexOf(s.substring(i, i + cc)) == -1)

result.appendCodePoint(cp);

i += cc;

}

return result.toString();

}

In the main method, make an ArrayBlockingQueue of capacity 1000. Change the newFixedThreadPool call to have 3 threads. Add the three runnables:

producePrimes(new BigInteger("1000000000000000"), 500_000, queue);

producePrimes(new BigInteger("1000000000500000"), 500_000, queue);

consumePrimes(queue, ...);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How to find if any no. is divisble by 4 or not ?

Answered: 1 week ago

Question

Explain the Pascals Law ?

Answered: 1 week ago

Question

4. Make recommendations on steps to take to melt the glass ceiling.

Answered: 1 week ago

Question

1. Discuss the potential legal issues that relate to training.

Answered: 1 week ago

Question

3. Design a program for preparing for cross-cultural assignments.

Answered: 1 week ago