<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Guava 中的 Stopwatch 是個(gè)什么鬼?

          共 3406字,需瀏覽 7分鐘

           ·

          2020-10-16 15:00

          點(diǎn)擊上方藍(lán)色“程序猿DD”,選擇“設(shè)為星標(biāo)”

          回復(fù)“資源”獲取獨(dú)家整理的學(xué)習(xí)資料!

          Stopwatch 解釋為計(jì)時(shí)器,又稱秒表、停表,很明顯它是記錄時(shí)間的。


          # 如何使用


          Stopwatch stopwatch = Stopwatch.createStarted();   doSomething(); stopwatch.stop(); // optional
          long millis = stopwatch.elapsed(MILLISECONDS);// formatted string like "12.3 ms"}
          log.info("time: " + stopwatch);

          安卓使用:

          Stopwatch.createStarted(         new Ticker() {           public long read() {             return android.os.SystemClock.elapsedRealtime();            }          });}

          看了上面這段代碼,有人會(huì)說,不用Stopwatch 照樣可以實(shí)現(xiàn)執(zhí)行時(shí)間的統(tǒng)計(jì),比如:

             long startTime = System.currentTimeMillis();
          try { // 模擬業(yè)務(wù)邏輯 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
          System.out.println(System.currentTimeMillis() - startTime);

          確實(shí)是,這樣也能統(tǒng)計(jì)這段代碼的執(zhí)行時(shí)間,那么為什么還會(huì)有Stopwatch(我也有這種想法)


          官方稱不直接使用System#nanoTime是有一下幾個(gè)原因:


          • 時(shí)間源可以替代 可以重寫Ticker(下面會(huì)介紹)

          • nanoTime的返回值是納秒,返回的值沒有意義,Stopwatch抽象返回值


          下面從實(shí)現(xiàn)方式來分析下guava為什么會(huì)設(shè)計(jì)這么類


          # 源碼分析


          內(nèi)部有幾個(gè)成員變量

            //時(shí)間源 一般和Stopwatch一起使用,而不是單獨(dú)使用  private final Ticker ticker;  private boolean isRunning;  private long elapsedNanos;  private long startTick;

          先看下Ticker(是個(gè)abstract類) 都有什么:

            public static Ticker systemTicker() {    return SYSTEM_TICKER;  }
          private static final Ticker SYSTEM_TICKER = new Ticker() { @Override public long read() { // 實(shí)際上就是System.nanoTime(); return Platform.systemNanoTime(); } }; // 子類重寫 ???public?abstract?long?read();

          回到Stopwatch,看下它的構(gòu)造方式:


            public static Stopwatch createUnstarted() {    return new Stopwatch();  }
          /** * Creates (but does not start) a new stopwatch, using the specified time source. * * @since 15.0 */ public static Stopwatch createUnstarted(Ticker ticker) { return new Stopwatch(ticker); }
          /** * Creates (and starts) a new stopwatch using {@link System#nanoTime} as its time source. * * @since 15.0 */ public static Stopwatch createStarted() { return new Stopwatch().start(); } Stopwatch() { this.ticker = Ticker.systemTicker(); }
          Stopwatch(Ticker ticker) { this.ticker = checkNotNull(ticker, "ticker"); }
            

          包括創(chuàng)建不啟動(dòng),創(chuàng)建啟動(dòng)的構(gòu)造方式


          執(zhí)行流程


          start--> stop 或者 reset


          看下代碼,很簡單

           public Stopwatch start() {    // 先判斷是否處于執(zhí)行狀態(tài)    checkState(!isRunning, "This stopwatch is already running.");    isRunning = true;    // 初始化 當(dāng)前的納秒時(shí)間    startTick = ticker.read();    return this;  }
          public Stopwatch stop() { long tick = ticker.read(); checkState(isRunning, "This stopwatch is already stopped."); isRunning = false; elapsedNanos += tick - startTick; return this; } public Stopwatch reset() { elapsedNanos = 0; isRunning = false; return this;??}

          獲取結(jié)果的代碼:

            // 計(jì)算納秒  private long elapsedNanos() {    return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos;  }
          // 轉(zhuǎn)換其他單位 public long elapsed(TimeUnit desiredUnit) { return desiredUnit.convert(elapsedNanos(), NANOSECONDS); }
          還有一些單位轉(zhuǎn)換和toString方法,就不分析了


          # 總結(jié)


          • 支持TimeUnit,可以將計(jì)算后的時(shí)間轉(zhuǎn)換為各種單位 比如:stopwatch.elapsed(TimeUnit.SECONDS))

          • 同一個(gè)Stopwatch,可以重置,重復(fù)記錄

          • 時(shí)間源可以替代 可以重寫Ticker

          • 其他 Spring 也有StopWatch 實(shí)現(xiàn)方式差不多,不支持替換時(shí)間源和可以重置,支持毫秒和納秒,但是增加了Task的概念

          來源:https://my.oschina.net/lowkeysoft/blog/1595755


          往期推薦

          為什么 Redis 單線程能支撐高并發(fā)?

          華為又一戰(zhàn)略級生態(tài)啟程:華為IdeaHub 使能千行百業(yè)

          必須了解的 MySQL 三大日志

          說了低調(diào)...這下百度知道了...

          離職半年了,最近又開始被吐槽輸出不夠...


          掃一掃,關(guān)注我

          一起學(xué)習(xí),一起進(jìn)步

          每周贈(zèng)書,福利不斷

          深度內(nèi)容

          推薦加入


          最近熱門內(nèi)容回顧? ?#技術(shù)人系列

          瀏覽 26
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  激情视频国产乱伦 | 日本和韩国的黄色一级视频 | 91免费福利 | 精品无人区一区 | 九九视频黄片 |