[英]start and stop time to schedule and run all the threads in java
我有下面的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");
}
你的意思是这样的吗?
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");
}
}
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.