在启动一个新线程的时候会有一定时间的额外开销,因此并不是所有情况下都应使用多线程。如下的代码段进行简单实验探讨:
//TimeCostToStartNewThread.java
public class TimeCostToStartNewThread{
public static void main(String[] args){
long now1 = System.nanoTime();
for(int i = 0; i < 10000000; i++){// i依次取值100,1000,10000,100000,1000000,10000000
}
long current1 = System.nanoTime();
System.out.println("current - now 1 = " + (current1 - now1));
long now2 = System.nanoTime();......
阅读全文