Question: 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

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!