public class Program { public static void main(String[] args) throws InterruptedException { var sw = new StopWatch("模板测试"); sw.start("任务1"); Thread.sleep(1000 * 1); sw.stop(); sw.start("任务2"); Thread.sleep(1000 * 2); sw.stop(); sw.start("任务3"); Thread.sleep(1000 * 3); sw.stop(); //打印各子任务耗时比例 System.out.println(sw.prettyPrint()); System.out.println("所有任务总耗时:" + sw.getTotalTimeMillis() + "ms"); } }
输出如下:
StopWatch '模板测试': running time = 6030843500 ns --------------------------------------------- ns % Task name --------------------------------------------- 1013406000 017% 任务1 2004607200 033% 任务2 3012830300 050% 任务3 所有任务总耗时:6030ms
标签:java,Thread,stop,sw,start,任务,sleep,使用,stopwatch From: https://www.cnblogs.com/nanfei/p/16598631.html