Java计算程序运行时间

来源:互联网 发布:招聘淘宝处理中差评 编辑:程序博客网 时间:2024/03/29 21:34

不废话,直接代码。

方法一:多线程法

Thread js_time = new Thread(new PP());js_time.start();//执行程序js_time.stop();

PP类:

class PP implements Runnable {public void run() {int i = 0;while (true) {try {Thread.sleep(2000);//睡眠2秒i = i + 2;} catch (InterruptedException e) {e.printStackTrace();}System.out.println("正在处理......已使用" + i + "秒");}}}


方法二:获取System时间

int t1 = (int) System.currentTimeMillis();//执行程序int t2 = (int) System.currentTimeMillis();System.out.println("已完成,耗时:" + Integer.toString(t2 - t1) + "毫秒");

有时间再加注释吧。

原创粉丝点击