StopWatch可以方便记录运行时间,主要用于单线程,单位为ms级,常用于日志记录运行时间。
举例
import org.apache.commons.lang3.time.StopWatch;
public class qwe {
public static void main(String[] args) {
// 初始化 (ms)
StopWatch started = StopWatch.createStarted();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long time = started.getTime();
// 1000
System.out.println(time);
addTime();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 获取目前运行时间
long endTime = started.getTime();
// 总时间:4001
System.out.println(endTime);
}
private static void addTime(){
StopWatch started = StopWatch.createStarted();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 2000
System.out.println(started.getTime());
}
}