apache StopWatch基本使用

pom:

<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.6</version>
</dependency>
org.apache.commons.lang.time.StopWatch

(1)

private static void test01() throws InterruptedException {
    StopWatch watch = new StopWatch();
    watch.start();
    Thread.sleep(1000);
    watch.split();
    /*
     * This is the time between start and latest split.
     * 调用start()方法到最后一次调用split()方法耗用的时间
     */
    System.out.println(watch.getSplitTime());
    Thread.sleep(2000);
    watch.split();
    System.out.println(watch.getSplitTime());
    Thread.sleep(500);
    watch.stop();
    /*
     * This is either the time between the start and the moment this method
     * is called, or the amount of time between start and stop
     * 调用start()方法到调用getTime()或stop()方法耗用的时间
     */
    System.out.println(watch.getTime());
}

打印信息:

1000
3000
3500

(2)

private static void test02() throws InterruptedException {
	StopWatch watch = new StopWatch();
	watch.start();
	Thread.sleep(1000);
	System.out.println(watch.getTime());
	/* 复位 归零 */
	watch.reset();
	watch.start();
	Thread.sleep(1000);
	System.out.println(watch.getTime());
}
打印信息:

1000
1000
(3)

private static void test03() throws InterruptedException {
	StopWatch watch = new StopWatch();
	watch.start();
	Thread.sleep(1000);
	System.out.println(watch.getTime());
	/* 暂停 */
	watch.suspend();
	System.out.println("do something");
	Thread.sleep(500);
	/* 恢复 */
	watch.resume();
	Thread.sleep(2000);
	System.out.println(watch.getTime());
}
打印信息:

1000
do something
3000



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值