1. 程式人生 > 其它 >Guava入門第七章(Stopwatch)

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
    }

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

作者:呂世昊 出處:https://www.cnblogs.com/lvshihao/p/15163191.html

個性簽名:學習如逆水行舟,不進則退!

如果覺得這篇文章對你有小小的幫助的話,記得在右下角點個“推薦”哦,博主在此感謝!