文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- Runtime
- 概念:
- Runtime类主要代表了应用程序的运行环境。一个RunTime就代表一个==运行时环境==。
- RunTime类常用的方法:
- (1) getRuntime():
- Runtime runtime = Runtime.getRuntime();
- (2) ==exec (String command)==:
- Process
- API--17--Process
- (3) freeMemory()
- (4) totalMemory()
- (5) maxMemory()
- (6) availableProcessors()
- (7) exit(int)方法
Runtime 概念:
Runtime类主要代表了应用程序的运行环境。一个RunTime就代表一个运行时环境。
- Runtime类是Java程序的运行时环境。不能new出一个Runtime对象,只能通过getRuntime()方法获取当前Runtime运行时对象的引用。
- 然后可以调用Runtime的方法查看和修改Java虚拟机的状态。
该方法用于返回当前应用程序的运行环境对象。
Runtime runtime = Runtime.getRuntime();(2) exec (String command):
- 该方法用于根据指定的路径执行对应的可执行文件。
- Runtime.getRuntime().exec(“ls”),就和cmd中执行ls效果一样了。
public class Demo7 {
public static void main(String[] args) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\Windows\notepad.exe");//打开记事本程序,并返回一个进程
Thread.sleep(3000); //让当前程序停止3秒。
process.destroy();
}
}
Process
API–17–Process(3) freeMemory()
可以查看当前虚拟机内存中空闲内存还有多少。
(4) totalMemory()可以查看当前虚拟机使用的总内存大小。
(5) maxMemory()可以查看JVM的最终可以使用的最大内存是多少。
(6) availableProcessors()可以查看本机有多少处理器,即本机处理器是多少核。
(7) exit(int)方法可以退出当前Java程序的运行,System.exit(int)方法就是调用了Runtime.exit(int)方法来退出运行的。



