【发布时间】:2016-06-11 14:26:19
【问题描述】:
我想测量完整的执行时间(所以当所有线程都完成时)。
System.currentimeMillis 的技巧在这里不起作用,因为当主方法结束时,我自己创建的线程仍然会运行,因为它们需要比主方法更长的时间来处理。
我该怎么做?
我举个例子。
public class Main {
public static void main(String[] args) {
long start = System.currentTimeMillis();
new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
long end = System.currentTimeMillis();
System.out.println(end - start); // Won't work because my new Thread will still be running here.
}
}
【问题讨论】:
标签: java multithreading time measure