<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 {@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>* @author Rod Johnson</p>
<p>* @author Juergen Hoeller</p>
<p>* @author Sam Brannen</p>
<p>* @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 = true;</p>
<p>private final List taskList = 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("");</p>
<p>}</p>
<p>/**</p>
<p>* Construct a new stop watch with the given id.</p>
<p>* Does not start any task.</p>
<p>* @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 = id;</p>
<p>}</p>
<p>/**</p>
<p>* Return the id of this stop watch, as specified on construction.</p>
<p>* @return the id (empty String by default)</p>
<p>* @since 4.2.2</p>
<p>* @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>* "false" when using a StopWatch for millions of intervals, or the task</p>
<p>* info structure will consume excessive memory. Default is "true".</p>
<p>*/</p>
<p>public void setKeepTaskList(boolean keepTaskList) {<!-- --></p>
<p>this.keepTaskList = keepTaskList;</p>
<p>}</p>
<p>/**</p>
<p>* Start an unnamed task. The results are undefined if {@link #stop()}</p>
<p>* or timing methods are called without invoking this method.</p>
<p>* @see #stop()</p>
<p>*/</p>
<p>public void start() throws IllegalStateException {<!-- --></p>
<p>start("");</p>
<p>}</p>
<p>/**</p>
<p>* Start a named task. The results are undefined if {@link #stop()}</p>
<p>* or timing methods are called without invoking this method.</p>
<p>* @param taskName the name of the task to start</p>
<p>* @see #stop()</p>
<p>*/</p>
<p>public void start(String taskName) throws IllegalStateException {<!-- --></p>
<p>if (this.running) {<!-- --></p>
<p>throw new IllegalStateException("Can't start StopWatch: it's already running");</p>
<p>}</p>
<p>this.running = true;</p>
<p>this.currentTaskName = taskName;</p>
<p>this.startTimeMillis = 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>* {@code start()} / {@code stop()} methods.</p>
<p>* @see #start()</p>
<p>*/</p>
<p>public void stop() throws IllegalStateException {<!-- --></p>
<p>if (!this.running) {<!-- --></p>
<p>throw new IllegalStateException("Can't stop StopWatch: it's not running");</p>
<p>}</p>
<p>long lastTime = System.currentTimeMillis() - this.startTimeMillis;</p>
<p>this.totalTimeMillis += lastTime;</p>
<p>this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);</p>
<p>if (this.keepTaskList) {<!-- --></p>
<p>this.taskList.add(lastTaskInfo);</p>
<p>}</p>
<p>++this.taskCount;</p>
<p>this.running = false;</p>
<p>this.currentTaskName = null;</p>
<p>}</p>
<p>/**</p>
<p>* Return whether the stop watch is currently running.</p>
<p>* @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>* @since 4.2.2</p>
<p>* @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 |
|