1
static final int Count=10;
static final int Number=10_0000_0000;
2
static final int Count=10;
static final int Number=10000;
package why;
public class Case1 {
static final int Count=10;
static final int Number=10_0000_0000;
static long sum(){
long r=0;
for (long i=0;i<Number;i++){
r+=i;
}
return r;
}
static void 单线程(){
long b=System.nanoTime();
for (int i = 0; i <Count ; i++) {
System.out.println(sum());
}
long e=System.nanoTime();
double s=(e-b)/1000_000_000.0;
System.out.printf("单线程,运行时间:%f%n",s);
}
static class SumThread extends Thread{
@Override
public void run(){
System.out.println(sum());
}
}
static void 多线程() throws InterruptedException{
long b=System.nanoTime();
Thread[] threads=new Thread[Count - 1];
for (int i = 0; i <Count-1 ; i++) {
threads[i]=new SumThread();
threads[i].start();
}
System.out.println(sum());
for (int i = 0; i < Count-1; i++) {
threads[i].join();
}
long e=System.nanoTime();
double s=(e-b)/1000_000_000.0;
System.out.printf("多线程,运行时间:%f%n",s);
}
public static void main(String[] args) throws InterruptedException {
单线程();
System.out.println("===========================");
多线程();
}
}