您的位置 首页 java

java计时器StopWatch使用方法

1.传统写法:

public static void main(String[] args) {
 long startTime=System.currentTimeMillis(); //获取开始时间 
 //函数主体代码
 long endTime=System.currentTimeMillis(); //获取结束时间
 System.out.println("程序运行时间: "+(endTime-startTime)+"ms"); 
}
 

2.使用stopWatch改进写法

引入坐标:
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.6</version>
</dependency>

代码:
public static void main(String[] args) {
 //创建并启动StopWatch
 StopWatch stopwatch = StopWatch.createStarted();
 //业务逻辑......
 stopwatch.stop();
 System.out.println(stopwatch.getTime(TimeUnit.MINUTES));
}
 

文章来源:智云一二三科技

文章标题:java计时器StopWatch使用方法

文章地址:https://www.zhihuclub.com/193622.shtml

关于作者: 智云科技

热门文章

网站地图