stopwatch java_Java计时器StopWatch实现方法代码实例

论坛 期权论坛     
选择匿名的用户   2021-5-30 01:11   71   0
<div style="font-size:16px;">
<p>下面提供三种计时器的写法供大家参考,大家可以自行选择自己钟爱的使用。</p>
<p>写法一(Spring 包提供的计时器):</p>
<p>import java.text.NumberFormat;</p>
<p>import java.util.LinkedList;</p>
<p>import java.util.List;</p>
<p>/**</p>
<p>* Simple stop watch, allowing for timing of a number of tasks,</p>
<p>* exposing total running time and running time for each named task.</p>
<p>*</p>
<p>* </p>
<p>Conceals use of {&#64;code System.currentTimeMillis()}, improving the</p>
<p>* readability of application code and reducing the likelihood of calculation errors.</p>
<p>*</p>
<p>* </p>
<p>Note that this object is not designed to be thread-safe and does not</p>
<p>* use synchronization.</p>
<p>*</p>
<p>* </p>
<p>This class is normally used to verify performance during proof-of-concepts</p>
<p>* and in development, rather than as part of production applications.</p>
<p>*</p>
<p>* &#64;author Rod Johnson</p>
<p>* &#64;author Juergen Hoeller</p>
<p>* &#64;author Sam Brannen</p>
<p>* &#64;since May 2, 2001</p>
<p>*/</p>
<p>public class StopWatch {<!-- --></p>
<p>/**</p>
<p>* Identifier of this stop watch.</p>
<p>* Handy when we have output from multiple stop watches</p>
<p>* and need to distinguish between them in log or console output.</p>
<p>*/</p>
<p>private final String id;</p>
<p>private boolean keepTaskList &#61; true;</p>
<p>private final List taskList &#61; new LinkedList();</p>
<p>/** Start time of the current task */</p>
<p>private long startTimeMillis;</p>
<p>/** Is the stop watch currently running? */</p>
<p>private boolean running;</p>
<p>/** Name of the current task */</p>
<p>private String currentTaskName;</p>
<p>private TaskInfo lastTaskInfo;</p>
<p>private int taskCount;</p>
<p>/** Total running time */</p>
<p>private long totalTimeMillis;</p>
<p>/**</p>
<p>* Construct a new stop watch. Does not start any task.</p>
<p>*/</p>
<p>public StopWatch() {<!-- --></p>
<p>this(&#34;&#34;);</p>
<p>}</p>
<p>/**</p>
<p>* Construct a new stop watch with the given id.</p>
<p>* Does not start any task.</p>
<p>* &#64;param id identifier for this stop watch.</p>
<p>* Handy when we have output from multiple stop watches</p>
<p>* and need to distinguish between them.</p>
<p>*/</p>
<p>public StopWatch(String id) {<!-- --></p>
<p>this.id &#61; id;</p>
<p>}</p>
<p>/**</p>
<p>* Return the id of this stop watch, as specified on construction.</p>
<p>* &#64;return the id (empty String by default)</p>
<p>* &#64;since 4.2.2</p>
<p>* &#64;see #StopWatch(String)</p>
<p>*/</p>
<p>public String getId() {<!-- --></p>
<p>return this.id;</p>
<p>}</p>
<p>/**</p>
<p>* Determine whether the TaskInfo array is built over time. Set this to</p>
<p>* &#34;false&#34; when using a StopWatch for millions of intervals, or the task</p>
<p>* info structure will consume excessive memory. Default is &#34;true&#34;.</p>
<p>*/</p>
<p>public void setKeepTaskList(boolean keepTaskList) {<!-- --></p>
<p>this.keepTaskList &#61; keepTaskList;</p>
<p>}</p>
<p>/**</p>
<p>* Start an unnamed task. The results are undefined if {&#64;link #stop()}</p>
<p>* or timing methods are called without invoking this method.</p>
<p>* &#64;see #stop()</p>
<p>*/</p>
<p>public void start() throws IllegalStateException {<!-- --></p>
<p>start(&#34;&#34;);</p>
<p>}</p>
<p>/**</p>
<p>* Start a named task. The results are undefined if {&#64;link #stop()}</p>
<p>* or timing methods are called without invoking this method.</p>
<p>* &#64;param taskName the name of the task to start</p>
<p>* &#64;see #stop()</p>
<p>*/</p>
<p>public void start(String taskName) throws IllegalStateException {<!-- --></p>
<p>if (this.running) {<!-- --></p>
<p>throw new IllegalStateException(&#34;Can&#39;t start StopWatch: it&#39;s already running&#34;);</p>
<p>}</p>
<p>this.running &#61; true;</p>
<p>this.currentTaskName &#61; taskName;</p>
<p>this.startTimeMillis &#61; System.currentTimeMillis();</p>
<p>}</p>
<p>/**</p>
<p>* Stop the current task. The results are undefined if timing</p>
<p>* methods are called without invoking at least one pair</p>
<p>* {&#64;code start()} / {&#64;code stop()} methods.</p>
<p>* &#64;see #start()</p>
<p>*/</p>
<p>public void stop() throws IllegalStateException {<!-- --></p>
<p>if (!this.running) {<!-- --></p>
<p>throw new IllegalStateException(&#34;Can&#39;t stop StopWatch: it&#39;s not running&#34;);</p>
<p>}</p>
<p>long lastTime &#61; System.currentTimeMillis() - this.startTimeMillis;</p>
<p>this.totalTimeMillis &#43;&#61; lastTime;</p>
<p>this.lastTaskInfo &#61; new TaskInfo(this.currentTaskName, lastTime);</p>
<p>if (this.keepTaskList) {<!-- --></p>
<p>this.taskList.add(lastTaskInfo);</p>
<p>}</p>
<p>&#43;&#43;this.taskCount;</p>
<p>this.running &#61; false;</p>
<p>this.currentTaskName &#61; null;</p>
<p>}</p>
<p>/**</p>
<p>* Return whether the stop watch is currently running.</p>
<p>* &#64;see #currentTaskName()</p>
<p>*/</p>
<p>public boolean isRunning() {<!-- --></p>
<p>return this.running;</p>
<p>}</p>
<p>/**</p>
<p>* Return the name of the currently running task, if any.</p>
<p>* &#64;since 4.2.2</p>
<p>* &#64;see #isRunning()</p>
<p>*/</p>
<p>public String currentTaskName() {<!-- --></p>
<p>return this.currentTaskName;</p>
<p>}</p>
<p>/**</p>
<p>* Return the time taken by the last task.</p>
<p>*/</p
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP