Runtime类 Runtime类用于表示java虚拟机运行时的状态,它用于封装java虚拟机进程。
通过getRuntime( )方法获取与之相关的Runtime对象
avaiProcessors( )方法和freeMemory( )方法和maxMemory()方法 代码实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 COPY package page03; import java.io.IOException; public class Strinh { public static void main(String[] args) throws IOException, InterruptedException { /*表示java虚拟机运行时的状态*/ /*Runtime类中提供】一个exec()方法,这个方法执行DOS命令*/ Runtime rt = Runtime.getRuntime(); /*availablePracessors()方法用来获得处理器的个数*/ /*freeMemory()方法用来获得空闲内存大小*/ /*maxMemory用来获得最大可用内存大小*/ System.out.println("处理器的个数:"+rt.availableProcessors()+"个"); System.out.println("空闲内存大小:"+rt.freeMemory()/1024/1024+"M"); System.out.println("最大可用内存大小"+rt.maxMemory()/1021/1024+"M"); Process process = rt.exec("notepad.exe"); Thread.sleep(3000); process.destroy(); } }
语法格式:
Runtime run = Runtime.getRuntime();
exec( )方法 Runtime类中提供了一个exec( )方法,该方法用于执行一个DOS命令,从而实现和命令框中输入DOS命令相同的效果
destroy( )方法 Runtime类中提供的destroy( )方法,是用来关闭打开的文件