StopWatch任务执行时间监控
来源:互联网 发布:免费网页数据抓取工具 编辑:程序博客网 时间:2024/06/15 01:07
在最近代码codereview中,发现代码中大量使用了StopWatch 这个类去监视执行的时间。这里总结一下大概的介绍。StopWatch 在spring中和apache中均提供了类似的功能,这里分别介绍一下。
一、Apache下的StopWatch
StopWath是apache commons lang包下的一个任务执行时间监视器,具体位置是org.apache.commons.lang3.time,常用的api 如下:
主要方法:
start(); //开始计时
split(); //设置split点
getSplitTime(); //获取从start 到 最后一次split的时间
reset(); //重置计时
suspend(); //暂停计时, 直到调用resume()后才恢复计时
resume(); //恢复计时
stop(); //停止计时
getTime(); //统计从start到现在的计时
使用demo如下:
import org.apache.commons.lang3.time.StopWatch;public class StopWatchTest {public static void main(String[] args) throws InterruptedException {StopWatch watch = new StopWatch();watch.start();//统计从start开始经历的时间Thread.sleep(1000);System.out.println(watch.getTime());//统计计时点Thread.sleep(1000);watch.split();System.out.println(watch.getSplitTime());//统计计时点Thread.sleep(1000);watch.split();System.out.println(watch.getSplitTime());//复位后, 重新计时watch.reset();watch.start();Thread.sleep(1000);System.out.println(watch.getTime());//暂停 与 恢复watch.suspend();System.out.println("暂停2秒钟");Thread.sleep(2000);watch.resume();Thread.sleep(1000);watch.stop();System.out.println(watch.getTime());}}
二、spring中的stopwatch
该类存在于org.springframework.util.StopWatch ,特点是StopWatch该类在统计时间的时候,必须得前一个对象关闭才能创建新的StopWatch,并且在统计完成后,只需要将其输出,就可以像报表一样,显示统计的时间
在开发中,常用于统计时间的是 使用 System.currentTimeMillis();进行统计,并且当执行完毕后,
还需要相减,才能得到最终时间值,这里直接提供了获取功能
public static void main(String[] args) throws InterruptedException { StopWatch sw = new StopWatch(); sw.start("读取文件"); Thread.sleep(1000); sw.stop(); sw.start("文件删除"); Thread.sleep(100); sw.stop(); sw.start("文件拷贝"); Thread.sleep(10); sw.stop(); System.out.println(sw.prettyPrint()); long stime =System.currentTimeMillis(); Thread.sleep(1000); long etime =System.currentTimeMillis(); System.out.println("执行时间:"+(etime-stime));}
- StopWatch任务执行时间监控
- spring StopWatch 监控请求执行时间
- filter 中用spring StopWatch 监控请求执行时间
- 一个简单的任务执行时间监视器 StopWatch
- 一个简单的任务执行时间监视器 StopWatch
- 【Apache Commons Lang】StopWatch任务执行时间监视器
- spring提供的一个简单的任务执行时间监视器 StopWatch
- StopWatch 记录执行时间
- System.Diagnostics.Stopwatch 获取执行时间
- 使用Stopwatch检测代码块的执行时间
- java监控函数执行时间
- net 监控方法执行时间
- 监控sql语句执行时间
- 监控sql执行时间
- C# 使用Stopwatch监视计时器来计算函数执行时间
- quratz任务执行时间
- Stopwatch
- Stopwatch
- canvas作dashTo的函数封装
- jquery的.html()动态遍历html代码后,对原有的css和js无法控制问题
- 数学之路(1)
- path变量
- 创建、删除、备份数据库
- StopWatch任务执行时间监控
- (转)详解spring 每个jar的作用
- 在windows、linux中开启nginx的Gzip压缩大大提高页面、图片加载速度
- java volatile
- Ubuntu下安装Java,R,R-studio,R-studio Server
- COCO Detection Challenge
- SecureCRT 连接Ubuntu 16.04出现Connection timed out 的问题
- iOS 异常处理机制
- 蚂蚁金服漆远首谈刚完成的AI重大突破及紧缺人才,呼吁国内多点技术性强的大会,少点网红