I've read, for example in Java Concurrency in Practice, that "a program with only one thread can run on at most one processor at a time."
However, if I run a single-threaded java class on my 2.5 GHz Intel Core 2 Duo Macbook Pro, I can see (in Activity Monitor) that the CPU usage shoots up from near 0 to around 60% on BOTH cores.
Is the mac OS somehow switching my thread from one core to another, and if so, why?
Thanks! Here's the code..
public class PrimeCalculator {
public static void findPrimes (int start, int end){
int countPrimes = 0;
for (int i=start; i<end; i++){
if (i % 1000000 == 0){
System.out.println("Did a million..");
}
boolean found = true;
for (int check = 2; check <= Math.sqrt(i); check++){
if (i % check == 0){
found = false;
break;
}
}
if (found == true){
//System.out.println(i);
countPrimes++;
}
}
System.out.println("I counted "+countPrimes+" primes");
}
public static void main(String[] args){
findPrimes(0,100000000);
}
}