在下面的程序中,为什么每次算出来的时间都是0;
请救大家帮个忙喽import java.*;
import java.util.*;
public class xc implements Runnable
{
int n;
long gt1,gt2,gt3;
public xc(int n)
{
gt1=0;
this.n=n;
}
public void run()
{
int i,j,k;
int a[]={7,9,20,1,14,12,25,22,6,99};
if(n==1)
{
synchronized(a)
{
System.out.println("Group(原数组):7 9 20 1 14 12 25 22 6 99");
System.out.println();
}
synchronized(a)
{ gt1=System.currentTimeMillis();
System.out.print("Group1(up to low): ");
for(i=0;i<=9;i++)
for(j=i+1;j<=9;j++)
{
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
gt2=System.currentTimeMillis();
gt3=gt1-gt2;
System.out.println("Elapsed milliseconds:" + gt3);
for(i=0;i<a.length;i++)
System.out.print(a[i]+" ");
} }
else
synchronized(a)
{
System.out.print("Group2(low to up): ");
gt1=System.currentTimeMillis();
for(i=0;i<=9;i++)
for(j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
gt2=System.currentTimeMillis();
for(i=0;i<=9;i++)
System.out.print(a[i]+" ");
System.out.println();
gt3=gt2-gt1;
System.out.println("Elapsed milliseconds:" + gt3);
}
}
public static void main(String args[])
{
xc r1=new xc(1);
xc r2=new xc(2);
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
t1.start();
t2.start();
}
}
请救大家帮个忙喽import java.*;
import java.util.*;
public class xc implements Runnable
{
int n;
long gt1,gt2,gt3;
public xc(int n)
{
gt1=0;
this.n=n;
}
public void run()
{
int i,j,k;
int a[]={7,9,20,1,14,12,25,22,6,99};
if(n==1)
{
synchronized(a)
{
System.out.println("Group(原数组):7 9 20 1 14 12 25 22 6 99");
System.out.println();
}
synchronized(a)
{ gt1=System.currentTimeMillis();
System.out.print("Group1(up to low): ");
for(i=0;i<=9;i++)
for(j=i+1;j<=9;j++)
{
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
gt2=System.currentTimeMillis();
gt3=gt1-gt2;
System.out.println("Elapsed milliseconds:" + gt3);
for(i=0;i<a.length;i++)
System.out.print(a[i]+" ");
} }
else
synchronized(a)
{
System.out.print("Group2(low to up): ");
gt1=System.currentTimeMillis();
for(i=0;i<=9;i++)
for(j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
gt2=System.currentTimeMillis();
for(i=0;i<=9;i++)
System.out.print(a[i]+" ");
System.out.println();
gt3=gt2-gt1;
System.out.println("Elapsed milliseconds:" + gt3);
}
}
public static void main(String args[])
{
xc r1=new xc(1);
xc r2=new xc(2);
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
t1.start();
t2.start();
}
}
加上
try{Thread.sleep(1000);}catch(Exception e){}
再试试.另外gt3 = gt2 - gt1;不是gt3 = gt1 - gt2; 大侠我运行你的程序时吓了一跳,以为时间倒流了. :-)