上善若水
In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
posts - 146,comments - 147,trackbacks - 0
  1/**
  2 * 在C#中提供了一个专门用于简单测试运行时间的类StopWatch,
  3 * 但是貌似JDK中并没有提供这样的类,既然没有提供,那么自己动手丰衣足食了。
  4 * 参考网上代码,简单写了一个。
  5 * 
  6 * 后来查了以下,貌似commons-lang中已经提供了一个StopWatch的类了,
  7 * 而且那个类的功能写的更完善一些,这个类就当是一个学习的渐进过程了。
  8 * 
  9 * @author levin
 10 * @since 2011-07-07
 11 */

 12public class StopWatch {
 13    private long startTime;
 14    private long endTime;
 15    private boolean isRunning;
 16    
 17    /**
 18     * 构造{@link #StopWatch}实例,清除所有状态
 19     */

 20    public StopWatch() {
 21        reset();
 22    }

 23    
 24    /**
 25     * 调用{@link #start()}以开始计时,调用该方法时,确保{@link #StopWatch}是没有处于运行状态,
 26     * 即{@link #start()}方法还未调用,或已经调用了{@link #stop()}方法。否则会抛{@link #IllegalStateException}
 27     * 
 28     * @return {@link #StopWatch}
 29     * @since 2011-07-07
 30     */

 31    public StopWatch start() {
 32        if(isRunning) {
 33            throw new IllegalStateException("StopWath is already started");
 34        }

 35        
 36        startTime = System.currentTimeMillis();
 37        endTime = -1;
 38        isRunning = true;
 39        
 40        return this;
 41    }

 42    
 43    /**
 44     * 调用{@link #stop()}方法以停止计时,调用该方法时,确保{@link #StopWatch}已经处于运行状态,
 45     * 即已经调用{@link #start()}方法,否则会抛{@link #IllegalStateException}。
 46     * 调用该方法后,即可以使用{@link #getRunningTime()}、{@link #getRunningSecs()}方法以获取运行时间
 47     * 
 48     * @return {@link #StopWatch}
 49     * @since 2011-07-07
 50     */

 51    public StopWatch stop() {
 52        if(isRunning) {
 53            throw new IllegalStateException("StopWatch hasn't been started yet");
 54        }

 55        
 56        endTime = System.currentTimeMillis();
 57        isRunning = false;
 58        
 59        return this;
 60    }

 61    
 62    /**
 63     * 获取{@link #StopWatch}的运行时间,以毫秒为单位。如果还未调用{@link #stop()}方法,则返回当前时间
 64     * 和开始时间毫秒差;否则返回{@link #StopWatch}的Watch时间。
 65     * 
 66     * @return 获取{@link #StopWatch}的运行时间,以毫秒为单位
 67     * @since 2011-07-07
 68     */

 69    public long getElapsed() {
 70        if(isRunning) {
 71            return System.currentTimeMillis() - startTime;
 72        }
 else {
 73            return endTime - startTime;
 74        }

 75    }

 76    
 77    /**
 78     * 获取{@link #StopWatch}的运行时间,以秒为单位。其他和{@link #getElapsed()}方法类似。
 79     * 
 80     * @return 获取{@link #StopWatch}的运行时间,以秒为单位
 81     * @since 2011-07-07
 82     */

 83    public long getElapsedSecs() {
 84        return TimeUnit.MILLISECONDS.toSeconds(getElapsed());
 85    }

 86    
 87    /**
 88     * 获取{@link #StopWatch}的运行时间,即调用{@link #start()}到调用{@link #stop()}的时间间隔,以毫秒为单位。
 89     * 若调用此方法时,还未调用{@link #stop()}方法,则抛{@link #IllegalStateException}。
 90     * 若在调用{@link #start()}方法前调用该方法,则返回0
 91     * 
 92     * @return 获取{@link #StopWatch}的运行时间,即调用{@link #start()}到调用{@link #stop()}的时间间隔,以毫秒为单位
 93     * @since 2011-07-07
 94     */

 95    public long getRunningTime() {
 96        if(isRunning) {
 97            throw new IllegalStateException("StopWatch hasn't been stopped yet");
 98        }

 99        
100        return endTime - startTime;
101    }

102    
103    /**
104     * 获取{@link #StopWatch}的运行时间,以秒为单位。其他参考{@link #getRunningTime()}方法。
105     * 
106     * @return 获取{@link #StopWatch}的运行时间,以秒为单位
107     * @since 2011-07-07
108     */

109    public long getRunningSecs() {
110        return TimeUnit.MILLISECONDS.toSeconds(getRunningTime());
111    }

112    
113    
114    /**
115     * 清除当前所有状态
116     * 返回{@link #StopWatch}而不是void,是为了能实用方法链的模式
117     * 
118     * @return {@link #StopWatch}
119     * @since 2011-07-07
120     */

121    public StopWatch reset() {
122        startTime = -1;
123        endTime = -1;
124        isRunning = false;
125        
126        return this;
127    }

128}
posted on 2011-07-08 11:05 DLevin 阅读(914) 评论(0)  编辑  收藏 所属分类: CodeTools

只有注册用户登录后才能发表评论。


网站导航: