スレッド作成のコスト

思ったよりたいしたことない。
100コnewしてstartしても、7ms。
newが意外とコスト高かとも思ったがそうでもない(1ms)。
ちなみにスレッド10000コにしてもまだCPU振り切らないみたい。おうちのパソコンも進化したもんだ。

import java.util.ArrayList;
import java.util.List;

public class Hoge {

    static int count;

    public static void main(String[] args) {

        long time0 = System.nanoTime();

        List<Thread> threads = new ArrayList<Thread>();
        for(int i=0 ;i<100; i++){
            Thread thread = new Thread(){
                public void run(){
                    count++;
                }
            };
            threads.add(thread);
        }

        long time1 = System.nanoTime();
        
        for (Thread thread : threads) {
            thread.start();            
        }

        System.out.println("count="+count); 
        
        long time2 = System.nanoTime();
       
        System.out.println("time1=" + (time1-time0)/1000/1000 + "ms");
        System.out.println("time2=" + (time2-time0)/1000/1000 + "ms"); 
    }
}

実行結果

count=100
time1=1ms
time2=7ms