Using Spring StopWatch to record time of a long running task.
package com.logicbig.example;
import org.springframework.util.StopWatch;
public class SpringStopWatchExample {
public static void main (String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start();
//long task simulation
Thread.sleep(1000);
sw.stop();
System.out.println(sw.getTotalTimeMillis());
}
}
Output
1013
Starting a named task and recording task running time.
package com.logicbig.example;
import org.springframework.util.StopWatch;
public class SpringStopWatchExample2 {
public static void main (String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start("A");//setting a task name
//long task simulation
Thread.sleep(1000);
sw.stop();
System.out.println(sw.getLastTaskTimeMillis());
}
}
Output
1009
Running multiple tasks one by one and and using prettyPrint() to display tasks running times in millis.
package com.logicbig.example;
import org.springframework.util.StopWatch;
public class SpringStopWatchExample3 {
public static void main (String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start("A");
Thread.sleep(500);
sw.stop();
sw.start("B");
Thread.sleep(300);
sw.stop();
sw.start("C");
Thread.sleep(200);
sw.stop();
System.out.println(sw.prettyPrint());
}
}
Output
StopWatch '': running time (millis) = 1031
-----------------------------------------
ms % Task name
-----------------------------------------
00514 050% A
00302 029% B
00215 021% C