栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java计时器StopWatch实现方法代码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java计时器StopWatch实现方法代码实例

下面提供三种计时器的写法供大家参考,大家可以自行选择自己钟爱的使用。

写法一(Spring 包提供的计时器):

import java.text.NumberFormat;
import java.util.linkedList;
import java.util.List;


public class StopWatch {

  
  private final String id;

  private boolean keepTaskList = true;

  private final List taskList = new linkedList();

  
  private long startTimeMillis;

  
  private boolean running;

  
  private String currentTaskName;

  private TaskInfo lastTaskInfo;

  private int taskCount;

  
  private long totalTimeMillis;


  
  public StopWatch() {
    this("");
  }

  
  public StopWatch(String id) {
    this.id = id;
  }


  
  public String getId() {
    return this.id;
  }

  
  public void setKeepTaskList(boolean keepTaskList) {
    this.keepTaskList = keepTaskList;
  }


  
  public void start() throws IllegalStateException {
    start("");
  }

  
  public void start(String taskName) throws IllegalStateException {
    if (this.running) {
      throw new IllegalStateException("Can't start StopWatch: it's already running");
    }
    this.running = true;
    this.currentTaskName = taskName;
    this.startTimeMillis = System.currentTimeMillis();
  }

  
  public void stop() throws IllegalStateException {
    if (!this.running) {
      throw new IllegalStateException("Can't stop StopWatch: it's not running");
    }
    long lastTime = System.currentTimeMillis() - this.startTimeMillis;
    this.totalTimeMillis += lastTime;
    this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
    if (this.keepTaskList) {
      this.taskList.add(lastTaskInfo);
    }
    ++this.taskCount;
    this.running = false;
    this.currentTaskName = null;
  }

  
  public boolean isRunning() {
    return this.running;
  }

  
  public String currentTaskName() {
    return this.currentTaskName;
  }


  
  public long getLastTaskTimeMillis() throws IllegalStateException {
    if (this.lastTaskInfo == null) {
      throw new IllegalStateException("No tasks run: can't get last task interval");
    }
    return this.lastTaskInfo.getTimeMillis();
  }

  
  public String getLastTaskName() throws IllegalStateException {
    if (this.lastTaskInfo == null) {
      throw new IllegalStateException("No tasks run: can't get last task name");
    }
    return this.lastTaskInfo.getTaskName();
  }

  
  public TaskInfo getLastTaskInfo() throws IllegalStateException {
    if (this.lastTaskInfo == null) {
      throw new IllegalStateException("No tasks run: can't get last task info");
    }
    return this.lastTaskInfo;
  }


  
  public long getTotalTimeMillis() {
    return this.totalTimeMillis;
  }

  
  public double getTotalTimeSeconds() {
    return this.totalTimeMillis / 1000.0;
  }

  
  public int getTaskCount() {
    return this.taskCount;
  }

  
  public TaskInfo[] getTaskInfo() {
    if (!this.keepTaskList) {
      throw new UnsupportedOperationException("Task info is not being kept!");
    }
    return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
  }


  
  public String shortSummary() {
    return "StopWatch '" + getId() + "': running time (millis) = " + getTotalTimeMillis();
  }

  
  public String prettyPrint() {
    StringBuilder sb = new StringBuilder(shortSummary());
    sb.append('n');
    if (!this.keepTaskList) {
      sb.append("No task info kept");
    }
    else {
      sb.append("-----------------------------------------n");
      sb.append("ms   %   Task namen");
      sb.append("-----------------------------------------n");
      NumberFormat nf = NumberFormat.getNumberInstance();
      nf.setMinimumIntegerDigits(5);
      nf.setGroupingUsed(false);
      NumberFormat pf = NumberFormat.getPercentInstance();
      pf.setMinimumIntegerDigits(3);
      pf.setGroupingUsed(false);
      for (TaskInfo task : getTaskInfo()) {
 sb.append(nf.format(task.getTimeMillis())).append(" ");
 sb.append(pf.format(task.getTimeSeconds() / getTotalTimeSeconds())).append(" ");
 sb.append(task.getTaskName()).append("n");
      }
    }
    return sb.toString();
  }

  
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder(shortSummary());
    if (this.keepTaskList) {
      for (TaskInfo task : getTaskInfo()) {
 sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
 long percent = Math.round((100.0 * task.getTimeSeconds()) / getTotalTimeSeconds());
 sb.append(" = ").append(percent).append("%");
      }
    }
    else {
      sb.append("; no task info kept");
    }
    return sb.toString();
  }


  
  public static final class TaskInfo {

    private final String taskName;

    private final long timeMillis;

    TaskInfo(String taskName, long timeMillis) {
      this.taskName = taskName;
      this.timeMillis = timeMillis;
    }

    
    public String getTaskName() {
      return this.taskName;
    }

    
    public long getTimeMillis() {
      return this.timeMillis;
    }

    
    public double getTimeSeconds() {
      return (this.timeMillis / 1000.0);
    }
  }

}

下面写一个调用:

public static void main(String[] args) throws InterruptedException {
//    StopWatchTest.test0();
   StopWatchTest.test1();
}

public static void test1() throws InterruptedException {
   StopWatch sw = new StopWatch("test");
   sw.start("task1");
   // do something
  Thread.sleep(100);
  sw.stop();
  sw.start("task2");
  // do something
  Thread.sleep(200);
  sw.stop();
  System.out.println("sw.prettyPrint()~~~~~~~~~~~~~~~~~");
  System.out.println(sw.prettyPrint());
}

运行结果:

sw.prettyPrint()~~~~~~~~~~~~~~~~~
StopWatch 'test': running time (millis) = 308
-----------------------------------------
ms % Task name
-----------------------------------------
00104 034% task1
00204 066% task2
---------------------

start开始记录,stop停止记录,然后通过StopWatch的prettyPrint方法,可直观的输出代码执行耗时,以及执行时间百分比,瞬间感觉比之前的方式高大上了一个档次。

 除此之外,还有以下两个方法shortSummary,getTotalTimeMillis,查看程序执行时间。

写法二(apache.commons实现的计时器):

import java.util.concurrent.TimeUnit;


public class StopWatch {

  private static final long NANO_2_MILLIS = 1000000L;


  
  public static StopWatch createStarted() {
    final StopWatch sw = new StopWatch();
    sw.start();
    return sw;
  }

  
  private enum State {

    UNSTARTED {
      @Override
      boolean isStarted() {
 return false;
      }
      @Override
      boolean isStopped() {
 return true;
      }
      @Override
      boolean isSuspended() {
 return false;
      }
    },
    RUNNING {
      @Override
      boolean isStarted() {
 return true;
      }
      @Override
      boolean isStopped() {
 return false;
      }
      @Override
      boolean isSuspended() {
 return false;
      }
    },
    STOPPED {
      @Override
      boolean isStarted() {
 return false;
      }
      @Override
      boolean isStopped() {
 return true;
      }
      @Override
      boolean isSuspended() {
 return false;
      }
    },
    SUSPENDED {
      @Override
      boolean isStarted() {
 return true;
      }
      @Override
      boolean isStopped() {
 return false;
      }
      @Override
      boolean isSuspended() {
 return true;
      }
    };

    
    abstract boolean isStarted();

    
    abstract boolean isStopped();

    
    abstract boolean isSuspended();
  }

  
  private enum SplitState {
    SPLIT,
    UNSPLIT
  }
  
  private State runningState = State.UNSTARTED;

  
  private SplitState splitState = SplitState.UNSPLIT;

  
  private long startTime;

  
  private long startTimeMillis;

  
  private long stopTime;

  
  public StopWatch() {
    super();
  }

  
  public void start() {
    if (this.runningState == State.STOPPED) {
      throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
    }
    if (this.runningState != State.UNSTARTED) {
      throw new IllegalStateException("Stopwatch already started. ");
    }
    this.startTime = System.nanoTime();
    this.startTimeMillis = System.currentTimeMillis();
    this.runningState = State.RUNNING;
  }


  
  public void stop() {
    if (this.runningState != State.RUNNING && this.runningState != State.SUSPENDED) {
      throw new IllegalStateException("Stopwatch is not running. ");
    }
    if (this.runningState == State.RUNNING) {
      this.stopTime = System.nanoTime();
    }
    this.runningState = State.STOPPED;
  }

  
  public void reset() {
    this.runningState = State.UNSTARTED;
    this.splitState = SplitState.UNSPLIT;
  }

  
  public void split() {
    if (this.runningState != State.RUNNING) {
      throw new IllegalStateException("Stopwatch is not running. ");
    }
    this.stopTime = System.nanoTime();
    this.splitState = SplitState.SPLIT;
  }

  
  public void unsplit() {
    if (this.splitState != SplitState.SPLIT) {
      throw new IllegalStateException("Stopwatch has not been split. ");
    }
    this.splitState = SplitState.UNSPLIT;
  }

  
  public void suspend() {
    if (this.runningState != State.RUNNING) {
      throw new IllegalStateException("Stopwatch must be running to suspend. ");
    }
    this.stopTime = System.nanoTime();
    this.runningState = State.SUSPENDED;
  }

  
  public void resume() {
    if (this.runningState != State.SUSPENDED) {
      throw new IllegalStateException("Stopwatch must be suspended to resume. ");
    }
    this.startTime += System.nanoTime() - this.stopTime;
    this.runningState = State.RUNNING;
  }

  
  public long getTime() {
    return getNanoTime() / NANO_2_MILLIS;
  }

  
  public long getTime(final TimeUnit timeUnit) {
    return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS);
  }

  
  public long getNanoTime() {
    if (this.runningState == State.STOPPED || this.runningState == State.SUSPENDED) {
      return this.stopTime - this.startTime;
    } else if (this.runningState == State.UNSTARTED) {
      return 0;
    } else if (this.runningState == State.RUNNING) {
      return System.nanoTime() - this.startTime;
    }
    throw new RuntimeException("Illegal running state has occurred.");
  }

  
  public long getSplitTime() {
    return getSplitNanoTime() / NANO_2_MILLIS;
  }
  
  public long getSplitNanoTime() {
    if (this.splitState != SplitState.SPLIT) {
      throw new IllegalStateException("Stopwatch must be split to get the split time. ");
    }
    return this.stopTime - this.startTime;
  }

  
  public long getStartTime() {
    if (this.runningState == State.UNSTARTED) {
      throw new IllegalStateException("Stopwatch has not been started");
    }
    // System.nanoTime is for elapsed time
    return this.startTimeMillis;
  }

  
  @Override
  public String toString() {
    return DurationFormatUtils.formatDurationHMS(getTime());
  }

  
  public String toSplitString() {
    return DurationFormatUtils.formatDurationHMS(getSplitTime());
  }

  
  public boolean isStarted() {
    return runningState.isStarted();
  }

  
  public boolean isSuspended() {
    return runningState.isSuspended();
  }

  
  public boolean isStopped() {
    return runningState.isStopped();
  }

}

写法三(Scala函数写法):

import org.slf4j.LoggerFactory


object Debug {
 val LOGGER = LoggerFactory.getLogger(getClass)
 val counter = collection.mutable.Map[String, Int]() // label -> count
 val times = collection.mutable.Map[String, Long]() // label - time(ns)

 
 def trace[T](label: String)(codeBlock: => T) = {
  val t0 = System.nanoTime()
  val result = codeBlock
  val t1 = System.nanoTime()
  counter.get(label).map(_counter => counter.put(label, _counter + 1)).orElse(counter.put(label, 1))
  times.get(label).map(cost => times.put(label, cost + (t1 - t0))).orElse(times.put(label, t1 - t0))
  result
 }

 
 def info(): Unit = {
  LOGGER.warn("FinallyDone...")
  LOGGER.warn(s"counter:${counter}")
  LOGGER.warn(s"times:${times.map { case (label, cost) => (label, cost / 1000000)}}ms")
 }

 
 def reset(): Unit = {
  counter.clear()
  times.clear()
 }
}

参考下面测试代码:

java版本:


public class StopWatchDemo {
  public static void main(String[] args){
    Debug.trace("方法1调用次数及用时", ()->{
      try {
 Thread.sleep(2000);
      } catch (InterruptedException e) {
 e.printStackTrace();
      }
      return "";
    });

    Debug.trace("方法1调用次数及用时", ()->{
      try {
 Thread.sleep(2000);
      } catch (InterruptedException e) {
 e.printStackTrace();
      }
      return "";
    });

    Debug.trace("方法2调用次数及用时", ()->{
      try {
 Thread.sleep(2000);
      } catch (InterruptedException e) {
 e.printStackTrace();
      }
      return 10;
    });

    Debug.info();
  }
}

输出结果:

15:29:32.228 [main] WARN test.Debug$ - FinallyDone...
15:29:32.361 [main] WARN test.Debug$ - counter:Map(方法2调用次数及用时 -> 1, 方法1调用次数及用时 -> 2)
15:29:32.364 [main] WARN test.Debug$ - times:Map(方法2调用次数及用时 -> 2000, 方法1调用次数及用时 -> 4000)ms

scala版本:


object StopWatchTest {
 def main(args: Array[String]): Unit = {
  Debug.trace("方法1调用次数及用时")( Thread.sleep(200))
  Debug.trace("方法1调用次数及用时")( Thread.sleep(200))
  Debug.trace("方法2调用次数及用时")( Thread.sleep(200))
  Debug.info()
 }
}

输出结果:

15:43:58.601 [main] WARN test.stopwatch.Debug$ - FinallyDone...
15:43:58.735 [main] WARN test.stopwatch.Debug$ - counter:Map(方法2调用次数及用时 -> 1, 方法1调用次数及用时 -> 2)
15:43:58.739 [main] WARN test.stopwatch.Debug$ - times:Map(方法2调用次数及用时 -> 200, 方法1调用次数及用时 -> 400)ms

对比java版本与scala版本,是不是看到scala的简洁及强大了吧!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133349.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号