下面提供三种计时器的写法供大家参考,大家可以自行选择自己钟爱的使用。
写法一(Spring 包提供的计时器):
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
/**
* Simple stop watch, allowing for timing of a number of tasks,
* exposing total running time and running time for each named task.
*
*
Conceals use of {@code System.currentTimeMillis()}, improving the
* readability of application code and reducing the likelihood of calculation errors.
*
*
Note that this object is not designed to be thread-safe and does not
* use synchronization.
*
*
This class is normally used to verify performance during proof-of-concepts
* and in development, rather than as part of production applications.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
* @since May 2, 2001
*/
public class StopWatch {
/**
* Identifier of this stop watch.
* Handy when we have output from multiple stop watches
* and need to distinguish between them in log or console output.
*/
private final String id;
private boolean keepTaskList = true;
private final List taskList = new LinkedList();
/** Start time of the current task */
private long startTimeMillis;
/** Is the stop watch currently running? */
private boolean running;
/** Name of the current task */
private String currentTaskName;
private TaskInfo lastTaskInfo;
private int taskCount;
/** Total running time */
private long totalTimeMillis;
/**
* Construct a new stop watch. Does not start any task.
*/
public StopWatch() {
this("");
}
/**
* Construct a new stop watch with the given id.
* Does not start any task.
* @param id identifier for