Spring提供的StopWatch,可以用来检测代码的执行时间。可以代替long start = System.currentTimeMillis();写法。
import org.springframework.util.StopWatch
public class Program {
public static void main(String[] args) throws InterruptedException {
StopWatch 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
0013406000 012% 任务1
0004607200 052% 任务2
0012830300 036% 任务3
所有任务总耗时:6030ms
标签:Thread,sw,System,start,任务,使用,StopWatch From: https://www.cnblogs.com/yiranjames/p/17095729.html