Class StopWatch

java.lang.Object
org.miaixz.bus.core.center.date.StopWatch

public class StopWatch extends Object
秒表封装 此工具用于存储一组任务的耗时时间,并一次性打印对比。 比如:我们可以记录多段代码耗时时间,然后一次性打印(StopWatch提供了一个prettyString()函数用于按照指定格式打印出耗时)

此工具来自:https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/StopWatch.java

使用方法如下:

 StopWatch stopWatch = StopWatch.of("任务名称");

 // 任务1
 stopWatch.start("任务一");
 Thread.sleep(1000);
 stopWatch.stop();

 // 任务2
 stopWatch.start("任务二");
 Thread.sleep(2000);
 stopWatch.stop();

 // 打印出耗时
 Console.log(stopWatch.prettyPrint());

 
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • StopWatch

      public StopWatch()
      构造,不启动任何任务
    • StopWatch

      public StopWatch(String id)
      构造,不启动任何任务
      Parameters:
      id - 用于标识秒表的唯一ID
    • StopWatch

      public StopWatch(String id, boolean keepTaskList)
      构造,不启动任何任务
      Parameters:
      id - 用于标识秒表的唯一ID
      keepTaskList - 是否在停止后保留任务,false 表示停止运行后不保留任务
  • Method Details

    • of

      public static StopWatch of()
      创建计时任务(秒表)
      Returns:
      StopWatch
    • of

      public static StopWatch of(String id)
      创建计时任务(秒表)
      Parameters:
      id - 用于标识秒表的唯一ID
      Returns:
      StopWatch
    • getId

      public String getId()
      获取StopWatch 的ID,用于多个秒表对象的区分
      Returns:
      the ID 默认为空字符串
      See Also:
    • setKeepTaskList

      public void setKeepTaskList(boolean keepTaskList)
      设置是否在停止后保留任务,false 表示停止运行后不保留任务
      Parameters:
      keepTaskList - 是否在停止后保留任务
    • start

      public void start() throws IllegalStateException
      开始默认的新任务
      Throws:
      IllegalStateException - 前一个任务没有结束
    • start

      public void start(String taskName) throws IllegalStateException
      开始指定名称的新任务
      Parameters:
      taskName - 新开始的任务名称
      Throws:
      IllegalStateException - 前一个任务没有结束
    • stop

      public void stop() throws IllegalStateException
      停止当前任务
      Throws:
      IllegalStateException - 任务没有开始
    • isRunning

      public boolean isRunning()
      检查是否有正在运行的任务
      Returns:
      是否有正在运行的任务
      See Also:
    • currentTaskName

      public String currentTaskName()
      获取当前任务名,null 表示无任务
      Returns:
      当前任务名,null 表示无任务
      See Also:
    • getLastTaskTimeNanos

      public long getLastTaskTimeNanos() throws IllegalStateException
      获取最后任务的花费时间(纳秒)
      Returns:
      任务的花费时间(纳秒)
      Throws:
      IllegalStateException - 无任务
    • getLastTaskTimeMillis

      public long getLastTaskTimeMillis() throws IllegalStateException
      获取最后任务的花费时间(毫秒)
      Returns:
      任务的花费时间(毫秒)
      Throws:
      IllegalStateException - 无任务
    • getLastTaskName

      public String getLastTaskName() throws IllegalStateException
      获取最后的任务名
      Returns:
      任务名
      Throws:
      IllegalStateException - 无任务
    • getLastTaskInfo

      public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException
      获取最后的任务对象
      Returns:
      StopWatch.TaskInfo 任务对象,包括任务名和花费时间
      Throws:
      IllegalStateException - 无任务
    • getTotal

      public long getTotal(TimeUnit unit)
      获取所有任务的总花费时间
      Parameters:
      unit - 时间单位,null表示默认TimeUnit.NANOSECONDS
      Returns:
      花费时间
    • getTotalTimeNanos

      public long getTotalTimeNanos()
      获取所有任务的总花费时间(纳秒)
      Returns:
      所有任务的总花费时间(纳秒)
      See Also:
    • getTotalTimeMillis

      public long getTotalTimeMillis()
      获取所有任务的总花费时间(毫秒)
      Returns:
      所有任务的总花费时间(毫秒)
      See Also:
    • getTotalTimeSeconds

      public double getTotalTimeSeconds()
      获取所有任务的总花费时间(秒)
      Returns:
      所有任务的总花费时间(秒)
      See Also:
    • getTaskCount

      public int getTaskCount()
      获取任务数
      Returns:
      任务数
    • getTaskInfo

      public StopWatch.TaskInfo[] getTaskInfo()
      获取任务列表
      Returns:
      任务列表
    • shortSummary

      public String shortSummary()
      获取任务信息,类似于:
           StopWatch '[data]': running time = [total] ns
       
      Returns:
      任务信息
    • shortSummary

      public String shortSummary(TimeUnit unit)
      获取任务信息,类似于:
           StopWatch '[data]': running time = [total] [unit]
       
      Parameters:
      unit - 时间单位,null则默认为TimeUnit.NANOSECONDS
      Returns:
      任务信息
    • prettyPrint

      public String prettyPrint()
      生成所有任务的一个任务花费时间表,单位纳秒
      Returns:
      任务时间表
    • prettyPrint

      public String prettyPrint(TimeUnit unit)
      生成所有任务的一个任务花费时间表
      Parameters:
      unit - 时间单位,null则默认TimeUnit.NANOSECONDS 纳秒
      Returns:
      任务时间表
    • toString

      public String toString()
      Overrides:
      toString in class Object