日期:2014-05-20 浏览次数:20753 次
public class TestNumber {
public int test(int i){
return 0;//让传入的所有int类型数都返回0
}
public void method1(){
long start = System.currentTimeMillis();
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
boolean flag = true; //记录初始值flag
for(int i=min; i<max; i++){
if(test(i) != 0){
flag = false; //如果返回值不是0则改变为true;
break;
}
}
System.out.println(flag);
long end = System.currentTimeMillis();
System.out.println("方法运行时间:"+(end-start));
}
public static void main(String[] args) {
TestNumber tn = new TestNumber();
tn.method1();
}
}
public class TestThreadNumber implements Runnable{
public int test(int i){
return 0;
}
@Override
public void run() {
long start = System.currentTimeMillis();
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
boolean flag = true;
for(int i=1; i<max; i++){
if(test(i) != 0){
flag = false;
break;
}
}
System.out.println(flag);
long end = System.currentTimeMillis();
System.out.println("线程1运行时间:"+(end-start));
}
public void test(){
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
boolean flag = true;
for(int i=min; i<0; i++){
if(test(i) != 0){
flag = false;
break;
}
}
System.out.println(flag);
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
TestThreadNumber ttn = new TestThreadNumber();
Thread t = new Thread(ttn);
t.start(); //启动线程1
ttn.test();//主线程调用test方法
long end = System.currentTimeMillis();
System.out.println("主线程运行时间:"+(end-start));
}
}