Question: Using this Code in JAVA: import java.math.BigInteger; import java.util.concurrent.*; public class Primes { public static Runnable printPrimes(BigInteger start, long length) { return () -> {

Using this Code in JAVA:

import java.math.BigInteger;

import java.util.concurrent.*;

public class Primes {

public static Runnable printPrimes(BigInteger start, long length)

{

return () ->

{

BigInteger n = start;

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

{

if (n.isProbablePrime(100))

System.out.println(n);

n = n.add(BigInteger.ONE);

}

};

}

public static void main(String[] args) {

Runnable r1 = printPrimes(new BigInteger("1000000000000000"), 10000);

Runnable r2 = printPrimes(new BigInteger("2000000000000000"), 10000);

ExecutorService service = Executors.newFixedThreadPool(2);

service.execute(r1);

service.execute(r2);

service.shutdown();

}

}

Make a function countPrimes that returns a Callable. Make two callables:

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

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

Submit them both to the same ExecutorService as in the preceding example. Youll get two Future values. You can access these values by:

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

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

before calling:

service.shutdown();

Add these calls around the calls to service.submit:

long start = System.currentTimeMillis();

...

long end = System.currentTimeMillis();

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

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

}

2

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!