StopWatch是位于org.springframework.util包下的一个工具类,通过它可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。
正常情况下,我们如果需要看某段代码的执行耗时,会通过如下的方式进行查看:
public static void main(String[] args) throws InterruptedException { StopWatchTest.test0(); // StopWatchTest.test1(); } public static void test0() throws InterruptedException { long start = System.currentTimeMillis(); // do something Thread.sleep(100); long end = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); // do something Thread.sleep(200); long end2 = System.currentTimeMillis(); System.out.println("某某1执行耗时:" + (end - start)); System.out.println("某某2执行耗时:" + (end2 - start2)); }
运行结果:
某某1执行耗时:105
某某2执行耗时:203
该种方法通过获取执行完成时间与执行开始时间的差值得到程序的执行时间,简单直接有效,但想必写多了也是比较烦人的,尤其是碰到不可描述的代码时,会更加的让人忍不住多写几个bug聊表敬意,而且该结果也不够直观,此时会想是否有一个工具类,提供了这些方法,或者自己写个工具类,刚好可以满足这种场景,并且把结果更加直观的展现出来。
首先我们的需求如下:
根据该需求,我们可直接使用org.springframework.util包下的一个工具类StopWatch,通过该工具类,我们对上述代码做如下改造:
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,查看程序执行时间。
运行代码及结果:
System.out.println("sw.shortSummary()~~~~~~~~~~~~~~~~~"); System.out.println(sw.shortSummary()); System.out.println("sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~"); System.out.println(sw.getTotalTimeMillis());
运行结果
sw.shortSummary()~~~~~~~~~~~~~~~~~
StopWatch 'test': running time (millis) = 308
sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~
308
其实以上内容在该工具类中实现也极其简单,通过start与stop方法分别记录开始时间与结束时间,其中在记录结束时间时,会维护一个链表类型的tasklist属性,从而使该类可记录多个任务,最后的输出也仅仅是对之前记录的信息做了一个统一的归纳输出,从而使结果更加直观的展示出来。
优点:
缺点:
spring中StopWatch源码实现如下:
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<TaskInfo> taskList = new LinkedList(); private long startTimeMillis; private boolean running; private String currentTaskName; private StopWatch.TaskInfo lastTaskInfo; private int taskCount; private long totalTimeMillis; public StopWatch() { this.id = ""; } public StopWatch(String id) { this.id = id; } public void setKeepTaskList(boolean keepTaskList) { this.keepTaskList = keepTaskList; } public void start() throws IllegalStateException { this.start(""); } public void start(String taskName) throws IllegalStateException { if (this.running) { throw new IllegalStateException("Can't start StopWatch: it's already running"); } else { this.startTimeMillis = System.currentTimeMillis(); this.running = true; this.currentTaskName = taskName; } } public void stop() throws IllegalStateException { if (!this.running) { throw new IllegalStateException("Can't stop StopWatch: it's not running"); } else { long lastTime = System.currentTimeMillis() - this.startTimeMillis; this.totalTimeMillis += lastTime; this.lastTaskInfo = new StopWatch.TaskInfo(this.currentTaskName, lastTime); if (this.keepTaskList) { this.taskList.add(this.lastTaskInfo); } ++this.taskCount; this.running = false; this.currentTaskName = null; } } public boolean isRunning() { return this.running; } public long getLastTaskTimeMillis() throws IllegalStateException { if (this.lastTaskInfo == null) { throw new IllegalStateException("No tasks run: can't get last task interval"); } else { 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"); } else { return this.lastTaskInfo.getTaskName(); } } public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException { if (this.lastTaskInfo == null) { throw new IllegalStateException("No tasks run: can't get last task info"); } else { return this.lastTaskInfo; } } public long getTotalTimeMillis() { return this.totalTimeMillis; } public double getTotalTimeSeconds() { return (double) this.totalTimeMillis / 1000.0D; } public int getTaskCount() { return this.taskCount; } public StopWatch.TaskInfo[] getTaskInfo() { if (!this.keepTaskList) { throw new UnsupportedOperationException("Task info is not being kept!"); } else { return (StopWatch.TaskInfo[]) this.taskList.toArray(new StopWatch.TaskInfo[this.taskList.size()]); } } public String shortSummary() { return "StopWatch '" + this.id + "': running time (millis) = " + this.getTotalTimeMillis(); } public String prettyPrint() { StringBuilder sb = new StringBuilder(this.shortSummary()); sb.append('/n'); if (!this.keepTaskList) { sb.append("No task info kept"); } else { sb.append("-----------------------------------------/n"); sb.append("ms % Task name/n"); sb.append("-----------------------------------------/n"); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumIntegerDigits(5); nf.setGroupingUsed(false); NumberFormat pf = NumberFormat.getPercentInstance(); pf.setMinimumIntegerDigits(3); pf.setGroupingUsed(false); StopWatch.TaskInfo[] var7; int var6 = (var7 = this.getTaskInfo()).length; for (int var5 = 0; var5 < var6; ++var5) { StopWatch.TaskInfo task = var7[var5]; sb.append(nf.format(task.getTimeMillis())).append(" "); sb.append(pf.format(task.getTimeSeconds() / this.getTotalTimeSeconds())).append(" "); sb.append(task.getTaskName()).append("/n"); } } return sb.toString(); } @Override public String toString() { StringBuilder sb = new StringBuilder(this.shortSummary()); if (this.keepTaskList) { StopWatch.TaskInfo[] var5; int var4 = (var5 = this.getTaskInfo()).length; for (int var3 = 0; var3 < var4; ++var3) { StopWatch.TaskInfo task = var5[var3]; sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis()); long percent = Math.round(100.0D * task.getTimeSeconds() / this.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 (double) this.timeMillis / 1000.0D; } } }
到此这篇关于Spring计时器StopWatch使用示例的文章就介绍到这了,更多相关Spring计时器StopWatch内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
时间:2020-05-12
Spring中添加计时器的时候根据业务需求可能会需要动态处理触发时间; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.EnableS
具有数据收集.校验和提交功能的表单生成器,包含复选框.单选框.输入框.下拉选择框等元素以及,省市区三级联动,时间选择,日期选择,颜色选择,文件/图片上传功能,支持事件扩展. 欢迎大家star学习交流:github地址 示例 https://raw.githubusercontent.com/xaboy/form-create/dev/images/sample110.jpg 安装 npm install form-create OR git clone https://github.com/xa
数据缓存原因:有些数据比较多,如果每次访问都要进行查询,无疑给数据库带来太大的负担,将一些庞大的查询数据并且更新次数较少的数据存入redis,能为系统的性能带来良好的提升. 业务逻辑思路:登入系统,访问数据时,检查redis是否有缓存,有则直接从redis中提取,没有则从数据库查询出,并存入redis中做缓存. 为什么要用redis做缓存: (1)异常快速:Redis的速度非常快,每秒能执行约11万集合,每秒约81000+条记录. (2)支持丰富的数据类型:Redis支持最大多数开发人员已经知道
Spring AOP 基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: <beans xmlns:aop="http://www.springframework.org/schema/aop"...> <!--启动支持--> <aop:aspectj-autoproxy /> </beans> 也可以配置AnnotationAwareAspectJAutoProxyCreator Bean来启动Spring对@
我这个主要是缩短Toast显示时间,要延长时间的话,可自行更改 废话不多说哈,见代码 import android.content.Context; import android.os.CountDownTimer; import android.util.Log; import android.widget.Toast; public class ToastUtil { private String TAG = "ToastUtil"; private Toast mToast; p
整理文档,搜刮出一个JavaScript实现的可变动态数字键盘控件方式实例代码,稍微整理精简一下做下分享. @sunRainAmazing JavaScript编写和实现的可变动态键盘密码输入控件,可以动态的生产数字键盘并显示,并且可以实现每次点击后密码键盘重新加载,可以手动刷新功能. 第一种方式,点击查看: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&qu
一 province.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <script type="text/javascript" language="javaScript"> var xmlHttp = false; //全局变量,
整理文档,搜刮出一个vue时间格式化实例代码,稍微整理精简一下做下分享. export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); } let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h
大家先看看页面效果吧,当当当当``````````````````````` 图中第二个选题是小颖点击了"新增选题"按钮,然后出来的,当你点击了"删除选项"或"删除选题"按钮,就会删除相应的选项和选题. html代码 <template> <div class="main-container"> <div class="form-horizontal"> <temp
一.Js代码 function getTime(){ str = "当前系统时间:" var p = document.getElementById("sy_time"); time = new Date(); year = time.getFullYear(); month = time.getMonth() + 1; day = time.getDate(); hour = time.getHours(); minutes = time.getMinutes()