1.) Analyze the Java code PrimeFinder.
_______________________________________________________________
Below is the "PrimeFinder" java code:
1: package com.java24hours;
2:
3: public class PrimeFinder implements Runnable {
4: Thread go;
5: StringBuffer primes = new StringBuffer();
6: int time = 0;
7:
8: public PrimeFinder() {
9: start();
10: while (primes != null) {
11: System.out.println(time);
12: try {
13: Thread.sleep(1000);
14: } catch (InterruptedException exc) {
15: // do nothing
16: }
17: time++;
18: }
19: }
20:
21: public void start() {
22: if (go == null) {
23: go = new Thread(this);
24: go.start();
25: }
26: }
27:
28: public void run() {
29: int quantity = 1_000_000;
30: int numPrimes = 0;
31: // candidate: the number that might be prime
32: int candidate = 2;
33: primes.append("\nFirst ").append(quantity).append("
primes:\n\n");
34: while (numPrimes < quantity) {
35: if (isPrime(candidate)) {
36: primes.append(candidate).append(" ");
37: numPrimes++;
38: }
39: candidate++;
40: }
41: System.out.println(primes);
42: primes = null;
43: System.out.println("\nTime elapsed: " + time + "
seconds");
44: }
45:
46: public static boolean isPrime(int checkNumber) {
47: double root = Math.sqrt(checkNumber);
48: for (int i = 2; i <= root; i++) {
49: if (checkNumber % i == 0) {
50: return false;
51: }
52: }
53: return true;
54: }
55:
56: public static void main(String[] arguments) {
57: new PrimeFinder();
58: }
59: }
To understand this, we need to undersatnd the concept of threads in Java.
Threads can be used to perform tasks faster. 2 Threads can run concurrently in Java. But here if we see the program in Debug mode-
There are 2 threads- 1. Main thread (for main method) and another is the thread which got started at line 24: go.start();
Main thread can be controlled by CurrentThread() method, which is not called anywhere in this program. So the main thread keeps on executing the below piece of code, untill primes gets some value from the run method. (Please note there are 2 threads working simultaneously. 1 running the below code and the other running the run() method)
while (primes != null) {
11: System.out.println(time);
12: try {
13: Thread.sleep(1000);
14: } catch (InterruptedException exc) {
15: // do nothing
16: }
17: time++;
18: }
So before the run() method thread could
calculate all the first 1000000 prime numbers. The main
thread gets into action and finds that primes is no longer
!= null and hence the while loop while (primes != null)
breaks, treminating the program. And thus we are not able
to see the prime numebrs.
If you want to see the prime numbers reduce the 29: int
quantity = 1_000_000; to 1000.
2. Why Thread.Sleep() ?
So If we do not use Thread.Sleep(), the main thread will keep
executing and will not give a chance to the run() thread to add
values to primes. So in this manner while (primes != null) will
always hold true and the program might go to an infinite loop.
Adding Thread.sleep() give a chance to the other thread to execute
and saves the program from going to infinite loop.
Output in my system- The output will depend on the RAM and storage and other performance of the laptop/desktop. So our outputs might varry.
The output gives
0
1
2
3
4
5
6
7
8
9
10
11
12
Get Answers For Free
Most questions answered within 1 hours.