StopWath是apache commons lang包下的一个任务执行时间监视器,它可以记录程序从开始执行到结束所花费的时间,
也可以记录程序中的某一段时间差。
主要的方法:
- start开始计时
- split设置split点
- getSplitTime获取从start 到 最后一次split的时间
- reset重置计时
- suspend暂停计时, 直到调用resume()后才恢复计时
- resume恢复计时
- stop停止计时
- getTime统计从start到现在的计时
具体的使用示例如下:
import org.apache.commons.lang3.time.StopWatch
public class Test {
public static void main(String[] args) throws Exception {
StopWatch watch = new StopWatch()
watch.start()
Thread.sleep(1000)
System.out.println("统计从开始到现在运行时间:"+watch.getTime()+"ms")
Thread.sleep(1000)
watch.split()
System.out.println("从开始到第一个切入点运行时间:"+watch.getSplitTime())
Thread.sleep(1000)
watch.split()
System.out.println("从开始到第二个切入点运行时间:"+watch.getSplitTime())
watch.reset()
//重置后必须使用start方法
watch.start()
Thread.sleep(1000)
System.out.println("重新开始后到当前运行时间是:"+watch.getTime())
watch.suspend()
System.out.println("暂停6秒钟")
Thread.sleep(6000)
watch.resume()
System.out.println("恢复后执行的时间是:"+watch.getTime())
watch.stop()
System.out.println("花费的时间》》"+watch.getTime()+"毫秒")
}
}