Guava入门第七章(Stopwatch)

Stopwatch详细介绍


package com.lvshihao.guava;

import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;

/**
 *@author: LVSHIHAO
 *@description: GUAVA Stopwatch timer introduction
 */
public class ElapsedEXAMPLE {

    public static void main(String[] args) throws InterruptedException {
        // 创建自动start的计时器
        Stopwatch watch = Stopwatch.createStarted();
        Thread.sleep(1000L);
        long time = watch.elapsed(TimeUnit.MILLISECONDS);
        // 结果1003
        System.out.println("代码执行时长:" + time);

        // 创建非自动start的计时器
        // 累计了start到stop的时间
        Stopwatch watch1 = Stopwatch.createUnstarted();
        watch1.start();
        Thread.sleep(1000L);
        watch1.stop();
        watch1.start();
        Thread.sleep(500L);
        time = watch1.elapsed(TimeUnit.MILLISECONDS);
        // 结果1500
        System.out.println("代码执行时长:" + time);

        Stopwatch watch2 = Stopwatch.createUnstarted();
        watch2.start();
        Thread.sleep(1000L);
        time = watch2.elapsed(TimeUnit.MILLISECONDS);
        // 结果1000
        System.out.println("代码执行时长:" + time);
        // 检查是否运行
        System.out.println(watch2.isRunning()); // true
    }

}

Guava入门第七章(Stopwatch)

上一篇:Data的方法


下一篇:顺序表与链表