我有以下的try方法,我需要弄清楚如何获得开始和停止时间来安排所有线程并打印出时间。
我知道如何获得实际的运行时间,只是无法弄清楚如何打印线程启动所花费的时间。感谢。
void run() {
long startTime = System.currentTimeMillis();
Code goes here.....
long endTime = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.println("Execution time is " + formatter.format((endTime - startTime) / 1000d) + " seconds");
}
答案 0 :(得分:0)
你的意思是这样的?
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MyThread extends Thread {
ThreadLocal<Long> timeAtStart = new ThreadLocal<Long>;
@Override
public synchronized void start() {
//override start method and print time when start method was invoked
timeAtStart.set(System.currentTimeMillis());
super.start();
}
@Override
public void run() {
long timeToRun = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.println("Starting thread took " + formatter.format((timeToRun - timeAtStart.get()) / 1000d) + " seconds");
//Code goes here.....
long endTime = System.currentTimeMillis();
System.out.println("Execution time is " + formatter.format((endTime - timeToRun) / 1000d) + " seconds");
}
}
感谢。