import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
@Slf4j
public class StopWatchTest {
public static void main(String[] args) throws InterruptedException {
StopWatch stopWatch = new StopWatch("每日工作时间");
stopWatch.start("上午");
forenoon();
stopWatch.stop();
stopWatch.start("午饭");
lunch();
stopWatch.stop();
stopWatch.start("午休");
noonBreak();
stopWatch.stop();
stopWatch.start("下午");
afternoon();
stopWatch.stop();
StopWatch.TaskInfo[] taskInfos = stopWatch.getTaskInfo();
for (StopWatch.TaskInfo taskInfo : taskInfos) {
log.info("{} 花费 {}s", taskInfo.getTaskName(), taskInfo.getTimeSeconds());
}
}
private static void forenoon() throws InterruptedException {
Thread.sleep(3000);
}
private static void lunch() throws InterruptedException {
Thread.sleep(1000);
}
private static void noonBreak() throws InterruptedException {
Thread.sleep(1000);
}
private static void afternoon() throws InterruptedException {
Thread.sleep(3000);
}
}
Spring StopWatch代码示例
最新推荐文章于 2024-03-12 14:38:36 发布