I have four threads running
t1.start
t2.start ... etc
Why is it doing this
2 is Prime.One
2 is Prime.Three
2 is Prime.Four
2 is Prime.Two
3 is Prime.Four
3 is Prime.Three
3 is Prime.One
5 is Prime.Three
5 is Prime.Four
3 is Prime.Two
7 is Prime.Four
7 is Prime.Three
package javaapplication9;
public class JavaApplication9 implements Runnable{
String name;
public JavaApplication9(String s) {
name = s;
}
int count = 1;
int num = 3;
int[] primes = new int[10];
public void run(){
primes[0] = 2;
// Find the first 10 primes
while (count<10) {
if (isPrime(num)) {
primes[count] = num;
count++;
}
num = num + 1;
}
// Prepares the output
for (int i=0; i<primes.length; i++) {
System.out.println (primes[i] + " is Prime." + name );
}}
public static boolean isPrime(int num) {
if (num%2==0) return false;
for(int i=3;i*i<=num;i+=2) {
if(num%i==0)
return false;
}
return true;
}}
This post has been edited by Rover2cool: 07 October 2012 - 05:22 PM

New Topic/Question
Reply




MultiQuote




|