Spring定时器StopWatch
来源:互联网 发布:剑三喵姐捏脸数据 编辑:程序博客网 时间:2024/06/05 06:33
简单总结一句,Spring提供的计时器StopWatch对于秒、毫秒为单位方便计时的程序,尤其是单线程、顺序执行程序的时间特性的统计输出支持比较好。也就是说假如我们手里面有几个在顺序上前后执行的几个任务,而且我们比较关心几个任务分别执行的时间占用状况,希望能够形成一个不太复杂的日志输出,StopWatch提供了这样的功能。而且Spring的StopWatch基本上也就是仅仅为了这样的功能而实现。
实际中用到的代码:
public void run() {LOGGER.info("[" + taskName + "]开始执行"); StopWatch stopWatch = new StopWatch(); stopWatch.start(taskName); try { doTask(); } catch (Exception e) { LOGGER.error("[" + taskName + "]执行失败", e); } finally { stopWatch.stop(); LOGGER.info("[" + taskName + "]执行结束,耗时:" + stopWatch.getTotalTimeMillis() + "毫秒"); }}
另外,还可以使用一些其他的方法:
import org.springframework.util.StopWatch; public class StopWatchDemo { /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub StopWatch clock = new StopWatch(); clock.start("TaskOneName"); Thread.sleep(1000 * 3);// 任务一模拟休眠3秒钟 clock.stop(); clock.start("TaskTwoName"); Thread.sleep(1000 * 10);// 任务一模拟休眠10秒钟 clock.stop(); clock.start("TaskThreeName"); Thread.sleep(1000 * 10);// 任务一模拟休眠10秒钟 clock.stop(); System.out.println(clock.prettyPrint()); } }
控制台输出如下:
StopWatch '': running time (millis) = 22926
-----------------------------------------
ms % Task name
-----------------------------------------
02990 013% TaskOneName
09968 043% TaskTwoName
09968 043% TaskThreeName
http://blog.csdn.net/ioe_gaoyong/article/details/22788789
阅读全文
0 0
- Spring定时器StopWatch
- spring stopwatch
- spring StopWatch 用法
- Spring中的计时器StopWatch
- Spring中的计时器StopWatch
- Spring计时器StopWatch的使用
- Spring中StopWatch的使用
- spring StopWatch 监控请求执行时间
- Stopwatch
- Stopwatch
- Stopwatch
- StopWatch
- Stopwatch
- filter 中用spring StopWatch 监控请求执行时间
- spring StopWatch 简单便捷的计时器
- spring定时器
- Spring定时器
- Spring 定时器
- Joda-Time 入门
- OOP三大特性及几大设计原则
- log4j的使用
- dwr中的部分问题和总结
- UltraEdit常用快捷键
- Spring定时器StopWatch
- LeetCode 633. Sum of Square Numbers
- TypeError: expected dynamic type 'string', but had type 'object'
- mybatis 教程(mybatis in action)
- Java中的session详解
- Hibernate之主键生成策略
- CF div2 C
- 帮你节省时间的100条建议
- Spring学习笔记(9)——注入参数