StopWatch是位于org.springframework.util包下的一个工具类,通过它可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。
代码:
public void test() throws Exception{
StopWatch stopWatch = new StopWatch("记录任务耗费时间");
stopWatch.start("业务一");
//doSomeThing one
Thread.sleep(100);
stopWatch.stop();
stopWatch.start("业务二");
//doSomeThing two
Thread.sleep(200);
stopWatch.stop();
System.out.println("任务耗时详情" + stopWatch.prettyPrint());
}
执行结果: