Stopwatch is interpreted as a timer, also known as stopwatch and stop watch. Obviously, it records time.
How to use
Stopwatch stopwatch = Stopwatch.createStarted(); doSomething(); stopwatch.stop(); // optional long millis = stopwatch.elapsed(MILLISECONDS); // formatted string like "12.3 ms"} log.info("time: " + stopwatch);
Android usage:
Stopwatch.createStarted( new Ticker() { public long read() { return android.os.SystemClock.elapsedRealtime(); } });}
After reading the above code, some people will say that the statistics of execution time can still be realized without Stopwatch, such as:
long startTime = System.currentTimeMillis(); try { // Analog business logic Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis() - startTime);
Indeed, this can also count the execution time of this code, so why is there Stopwatch (I also have this idea)
Officials say there are several reasons why System#nanoTime is not used directly:
- Time source can be replaced and Ticker can be rewritten (described below)
- The return value of nanoTime is nanoseconds. The return value is meaningless. Stopwatch abstracts the return value
The following is an analysis of why guava designed such a class from the implementation mode
Source code analysis
There are several member variables inside
//Time sources are generally used with Stopwatch rather than alone private final Ticker ticker; private boolean isRunning; private long elapsedNanos; private long startTick;
Let's take a look at what Ticker (an abstract class) has:
public static Ticker systemTicker() { return SYSTEM_TICKER; } private static final Ticker SYSTEM_TICKER = new Ticker() { @Override public long read() { // It's actually system nanoTime(); return Platform.systemNanoTime(); } }; // Subclass override public abstract long read();
Go back to Stopwatch and see how it is constructed:
public static Stopwatch createUnstarted() { return new Stopwatch(); } /** * Creates (but does not start) a new stopwatch, using the specified time source. * * @since 15.0 */ public static Stopwatch createUnstarted(Ticker ticker) { return new Stopwatch(ticker); } /** * Creates (and starts) a new stopwatch using {@link System#nanoTime} as its time source. * * @since 15.0 */ public static Stopwatch createStarted() { return new Stopwatch().start(); } Stopwatch() { this.ticker = Ticker.systemTicker(); } Stopwatch(Ticker ticker) { this.ticker = checkNotNull(ticker, "ticker"); }
It includes the construction method of creating startup without startup and creating startup
Execution process
start – > stop or reset
Look at the code. It's simple
public Stopwatch start() { // First judge whether it is in the execution state checkState(!isRunning, "This stopwatch is already running."); isRunning = true; // Initializes the current nanosecond time startTick = ticker.read(); return this; } public Stopwatch stop() { long tick = ticker.read(); checkState(isRunning, "This stopwatch is already stopped."); isRunning = false; elapsedNanos += tick - startTick; return this; } public Stopwatch reset() { elapsedNanos = 0; isRunning = false; return this; }
Code to get the result:
// Calculate nanosecond private long elapsedNanos() { return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos; } // Convert other units public long elapsed(TimeUnit desiredUnit) { return desiredUnit.convert(elapsedNanos(), NANOSECONDS); }
There are also some unit conversion and toString methods, which are not analyzed
summary
- Timeunit is supported, which can convert the calculated time into various units, such as stopwatch elapsed(TimeUnit.SECONDS))
- The same Stopwatch can be reset and recorded repeatedly
- Time source can be replaced and Ticker can be rewritten
- Other Spring also has similar StopWatch implementation methods. It does not support replacing time sources and resetting. It supports milliseconds and nanoseconds, but adds the concept of Task
last
Whether you want to leave or continue to insist, the most important thing is to get out of the comfort zone. Maybe we have only worked for two or three years without this feeling, but we should make long-term plans and take every step at our own pace. Xiaobian has prepared front-end interview materials and front-end learning mind map and materials for the interview partners, which are shared free of charge. I hope it will be helpful to you when you want to get out of trouble or leave the comfort zone.
I feel, but I have to make a long-term plan for everything and take every step at my own pace. Xiaobian has prepared front-end interview materials and front-end learning mind map and materials for the interview partners, which are shared free of charge. I hope it will be helpful to you when you want to get out of trouble or leave the comfort zone.
[external chain picture transferring... (img-lf8CZ9GK-1627103358844)]
[external chain picture transferring... (img-Y32dc1Vl-1627103358847)]