我希望测量完整的执行时间(即当所有线程都完成时)。
使用
我来举个例子。
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.
}
}