软讯网络 > 编程语言 > Java > Java程序运行时间
【标 题】:Java程序运行时间
【关键字】:
Java
【来 源】:http://www.cublog.cn/u/24359/showart.php?id=180965
Java程序运行时间
用for语句输出十个数字,使用Thread.currentThread().sleep()方法使Main线程睡眠一段时间,计算程序总共运行的时间,源码如下:
import java.util.*;
import java.text.*;
class ThreadTest
{
public static void main(String[] args)
{
Date start = new Date();
System.out.println(start.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy'年'MM'月'dd'日'E");
String str = df.format(start);
System.out.println(str);
for(int i=0;i<10;i++)
{
System.out.println(i);
try
{
Thread.currentThread().sleep(100); //抛出异常,必须被捕获
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
Date end = new Date();
System.out.println(end.getTime());
long span = end.getTime() - start.getTime();
System.out.println("The time Span is: "+span);
}
}