使用代碼以下java
import org.springframework.util.StopWatch; public class ApplicationTest4 { public static void main(String[] args) throws Exception { StopWatch watch = new StopWatch("這是一個測試用的StopWatch"); watch.start("起牀"); Thread.sleep(1000); watch.stop(); watch.start("洗漱"); Thread.sleep(2000); watch.stop(); watch.start("鎖門"); Thread.sleep(500); watch.stop(); System.out.println(watch.prettyPrint()); System.out.println(watch.getLastTaskName()); System.out.println(watch.getTaskCount()); System.out.println(watch.getLastTaskInfo()); } }
運行輸出結果見下:spring
StopWatch '這是一個測試用的StopWatch': running time (millis) = 3503 ----------------------------------------- ms % Task name ----------------------------------------- 01001 029% 起牀 02001 057% 洗漱 00501 014% 鎖門 鎖門 3 org.springframework.util.StopWatch$TaskInfo@3af49f1c
注意是在org.springframework.util 包下測試
方法也很好理解code
watch.prettyPrint() 打印完整的任務監控信息對象
watch.getLastTaskName() 獲取上一個任務的名稱get
watch.getTaskCount() 獲取任務個數io
watch.getLastTaskInfo() 獲取上一個任務對象ast