たとえば、Java Concurrency in Practice で、「スレッドが 1 つしかないプログラムは、一度に多くても 1 つのプロセッサで実行できる」と読んだことがあります。
ただし、2.5 GHz Intel Core 2 Duo Macbook Pro でシングルスレッドの Java クラスを実行すると、両方のコアで CPU 使用率がほぼ 0% から約 60% に上昇することが (Activity Monitor で) わかります。
Mac OS は何らかの方法でスレッドをあるコアから別のコアに切り替えていますか? もしそうなら、それはなぜですか?
ありがとう!これがコードです..
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);
}
}