我想測量完整的執行時間(所以當所有線程完成時)。 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