StopWatch计算计时工具类
2016-06-01 12:04
344 查看
/** * 秒表类,用于计算执行时间 * 注意该类是非线程安全的 * @author jxqlovejava * */ public class StopWatch { private static final String DEFAULT_TASK_NAME = "defaultTask"; private String taskName; private long start, end; private boolean hasStarted, hasEnded; // 时间单位枚举:毫秒、秒和分钟 public enum TimeUnit { MILLI, SECOND, MINUTE } public StopWatch() { this(DEFAULT_TASK_NAME); } public StopWatch(String taskName) { this.taskName = StringUtil.isEmpty(taskName) ? DEFAULT_TASK_NAME : taskName; } public void start() { start = System.currentTimeMillis(); hasStarted = true; } public void end() { if(!hasStarted) { throw new IllegalOperationException("调用StopWatch的end()方法之前请先调用start()方法"); } end = System.currentTimeMillis(); hasEnded = true; } public void clear() { this.start = 0; this.end = 0; this.hasStarted = false; this.hasEnded = false; } /** * 获取总耗时,单位为毫秒 * @return 消耗的时间,单位为毫秒 */ public long getEclapsedMillis() { if(!hasEnded) { throw new IllegalOperationException("请先调用end()方法"); } return (end-start); } /** * 获取总耗时,单位为秒 * @return 消耗的时间,单位为秒 */ public long getElapsedSeconds() { return this.getEclapsedMillis() / 1000; } /** * 获取总耗时,单位为分钟 * @return 消耗的时间,单位为分钟 */ public long getElapsedMinutes() { return this.getEclapsedMillis() / (1000*60); } public void setTaskName(String taskName) { this.taskName = StringUtil.isEmpty(taskName) ? DEFAULT_TASK_NAME : taskName; } public String getTaskName() { return this.taskName; } /** * 输出任务耗时情况,单位默认为毫秒 */ public void printEclapseDetail() { this.printEclapseDetail(TimeUnit.MILLI); } /** * 输出任务耗时情况,可以指定毫秒、秒和分钟三种时间单位 * @param timeUnit 时间单位 */ public void printEclapseDetail(TimeUnit timeUnit) { switch(timeUnit) { case MILLI: System.out.println(this.getTaskName() + "任务耗时(毫秒):" + this.getEclapsedMillis()); break; case SECOND: System.out.println(this.getTaskName() + "任务耗时(秒):" + this.getElapsedSeconds()); break; case MINUTE: System.out.println(this.getTaskName() + "任务耗时(分钟):" + this.getElapsedMinutes()); break; default: System.out.println(this.getTaskName() + "任务耗时(毫秒):" + this.getEclapsedMillis()); } } }
相关文章推荐
- Nginx服务器防止被压力测试
- python+Django+apache的配置方法详解
- linux混杂设备驱动
- tomcat的配置和优化
- 我与MVVM的恩怨情仇
- 可能会用到的网站
- 阿里云云服务器Linux系统FTP服务器搭建设置教程
- Linux学习之CentOS(二十六)--Linux磁盘管理:LVM逻辑卷的创建及使用
- TopShelf+Quartz.net实现基于window服务的定时调度
- tomcat 安装配置 解析jsp程序
- Centos必备文档
- Linux shell function call Sample
- open-falcon+grafana出图
- 微信开放平台开发(2) 网站应用微信登录
- linux主机名被更改为bogon原理分析
- Awstats +JAWstats(ftpshell) 对日志分析部署整合功能
- 文件的具体操作,fopen在不同场景的参数设置
- Tomcat 7最大并发连接数的正确修改方法
- 短信验证码是什么?在网站中起到什么作用?
- OpenCV HOG+SVM行人检测:从训练到检测