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
final long length) {
Callable
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
Callable
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
Future
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
Future
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
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
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
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started