Question

2.) Analyze the modified Java code PrimeFinderInterupt Make this program to RUN and provide a screenshot...

2.) Analyze the modified Java code PrimeFinderInterupt

  1. Make this program to RUN and provide a screenshot of the output
  2. Describe the additional functionality of the PrimeFinderInterrupt
    1. NOTE IF your original PrimeFinder ran faster than 7 seconds, change the code line: "if (++index > 7 )" to have a number less than the time to run your original code.

_______________________________________________________________

Below is the "PrimeFinderInterupt" java code:

package com.java24hours;

public class PrimeFinderInterrupt implements Runnable {
Thread go;
StringBuffer primes = new StringBuffer();
int time = 0;
int index = 1;

public PrimeFinderInterrupt() {
start();
while (primes != null) {
System.out.println(time);
if (++index > 3 )
{
System.out.println("cancel thread after 3 seconds... too long");
stop();
}
try {
Thread.sleep(1000);
} catch (InterruptedException exc) {
// do nothing
}
time++;
}
}

public void start() {
if (go == null) {
go = new Thread(this);
go.start();
}
}
  
public void stop()
{
go.interrupt();
}

public void run() {
int quantity = 1_000_000;
int numPrimes = 0;
// candidate: the number that might be prime
int candidate = 2;
primes.append("\nFirst ").append(quantity).append(" primes:\n\n");
  
while (numPrimes < quantity) {
if (isPrime(candidate)) {
primes.append(candidate).append(" ");
numPrimes++;
}
candidate++;

if (Thread.currentThread().isInterrupted())
{
System.out.println("Thread timeout... exiting");
primes = null;
return;
}
}
  
System.out.println(primes);
primes = null;
System.out.println("\nTime elapsed: " + time + " seconds");
}

public static boolean isPrime(int checkNumber) {
double root = Math.sqrt(checkNumber);
for (int i = 2; i <= root; i++) {
if (checkNumber % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] arguments) {
  
long startns = System.nanoTime();
long startms = System.currentTimeMillis();
double a=-12.225, b=4, c=0;
for (int idx = 0; idx < 5000; ++idx)
{
c = a + b;
}
long endns = System.nanoTime();
long endms = System.currentTimeMillis();
long diffns = endns-startns;
long dms = endms - startms;
System.out.println("time of 500 divides in ns and ms =" + diffns + " " + dms);
  
new PrimeFinderInterrupt();
}
}

Homework Answers

Answer #1

Addition function of primeInterrupt is a thread validation check that if thread takes more than time assigned time it stop that thread from running forever give control backs to main thread

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Analyze this code and run it, if there are any issues note them and fix them,...
Analyze this code and run it, if there are any issues note them and fix them, if not give the output and Big O notation runtime: public class PrintBits { public static void printBits(int a) { try { String str = Integer.toBinaryString((Integer) a); for(int i = 0; i < str.length(); i++){ System.out.print (str.charAt(i)); } } catch (ClassCastException e) { throw new RuntimeException ("Argument is not an Integer"); } } public static void main (String[] args){ printBits (-17); System.out.println(); printBits (17);...
I have a 2 class code and it works everything is fine and runs as it...
I have a 2 class code and it works everything is fine and runs as it supposed too. What will the UML be for both classes. Here's the code, any help will be awsome, Thanks. import java.util.Scanner; public class PayRollDemo { public static void main(String[] args) { double payRate; int hours; PayRoll pr = new PayRoll(); Scanner keyboard = new Scanner(System.in); for (int index = 0; index < 7 ; index++ ) { System.out.println(); System.out.println("EmployeeID:" + pr.getEmployeeID(index)); System.out.println(); System.out.println("Enter the...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
I have the following program which returns F(n) for the fibonacci sequence both recursively and iteratively:...
I have the following program which returns F(n) for the fibonacci sequence both recursively and iteratively: public class Fibonacci { //Recursive method public int fibRecursive(int n) { //If n is in the 0th or 1st place return n if (n <= 1) return n; return fibRecursive(n - 1) + fibRecursive(n - 2); } //Iterative method public int fibIterative(int n) { if (n <= 1) return n; int fib = 1, prevFib = 1; for (int i = 2; i <...
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
Question: I am using three different ways to execute this program. I am a bit confused...
Question: I am using three different ways to execute this program. I am a bit confused on the time complexity for each one. Can someone explain the time complexity for each function in relation to the nanoseconds. import java.util.HashMap; import java.util.Map; public class twosums {       public static void main(String args[]) {               int[] numbers = new int[] {2,3,9,4,8};;        int target = 10;               long nano_begin1 = System.nanoTime();        int[]...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999,...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT