我想测量完整的执行时间(所以当所有线程完成时)。 System.currentimeMillis
的技巧在这里不起作用,因为当main方法结束时,我自己创建的线程仍然会运行,因为它们比main方法处理需要更长的时间。 我该怎么做?测量多线程的执行时间
我举个例子。
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.
}
}
让我知道,如果这有助于。 – Learner