java的System和Runtime类的使用

System类

获取毫秒:System.currentTimeMillis();
获取纳秒:System.nanoTime();
拷贝数组:System.arraycopy(原数组, 从什么位置开始拷贝, 新数组, 新数组开始拷贝位置, 长度);
手动调用垃圾回收机制:System.gc();
获取分隔符:System.lineSeparator();
终止程序退出jvm:System.lineSeparator()。

package com.openlab.day16.main;

public class TestSystem {

	public static void main(String[] args) {
		
		
//		System.out.println(new Date().getTime());
		
		// 毫秒
		System.out.println(System.currentTimeMillis());
		// 纳秒
		System.out.println(System.nanoTime());
		
//		System.arraycopy(原数组, 从什么位置开始拷贝, 新数组, 新数组开始拷贝位置, 长度);
		
//		手动使用gc
		System.gc();
		
		System.out.println(System.lineSeparator());
		
		// 终止程序,JVM退出
		System.exit(0);
		
		
	}
}

Runtime类

Runtime类不能实例化,只能通过单例模式获取对象,即下列代码:Runtime runtime = Runtime.getRuntime();

package com.openlab.day16.main;

public class TestRuntim {
	
	public static void main(String[] args) throws Exception {
		
		Runtime runtime = Runtime.getRuntime();//单例模式
		
		// 返回当前运行时的JVM中运行的java进程
		System.out.println(runtime.availableProcessors());
		
		// JVM内存使用情况
		System.out.println(runtime.freeMemory());
		System.out.println(runtime.maxMemory());
		System.out.println(runtime.totalMemory());
		
		// System调用的退出系统、gc等方法,实质是Runtime中方法
		runtime.exec("calc");//打开计算器
//		runtime.exec("cls");//清屏
		runtime.exec("notepad");//打开记事本
		runtime.exec("mspaint");//打开画板
		runtime.exec("ipconfig");//获取ip地址
		
		
	}
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值