25System(应用)

# 1 System(应用)

  • System类的常用方法
方法名 说明
public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
  • 代码

需求:在控制台输出1-10000,计算这段代码执行了多少毫秒

public class SystemDemo {
    public static void main(String[] args) {
        // 获取开始的时间节点
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 10000; i++) {
                System.out.println(i);
            }
        // 获取代码运行结束后的时间节点
        long end = System.currentTimeMillis();
        System.out.println("共耗时:" + (end - start) + "毫秒");
    }
}
posted @ 2021-09-19 18:39  zjh1170  阅读(31)  评论(0)    收藏  举报