Common-lang任务执行时间监视器StopWatch

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()+"毫秒");

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值