Runtime类是运行时操作类,可以通过Runtime获取一些系统信息
实例:
public class Demo05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Runtime run=Runtime.getRuntime(); //Runtime实例
Process pro=null;
System.out.println("maxMemory:"+run.maxMemory()); //输出最大内存
System.out.println("freeMemory before gc:"+run.freeMemory());//输出可用内存
run.gc(); //主动调用垃圾回收
System.out.println("freeMemory after gc:"+run.freeMemory());
/*执行新线程,此处必须进行异常处理,*/
try{
pro=run.exec("notepad.exe");
System.out.println("open Succeed!");
}catch(Exception e){
System.out.println("open error!");
}
/*设置执行的notepad线程存活时间,此处必须进行异常处理*/
try{
Thread.sleep(10000); //打开的notepad.exe存活10s
}catch(Exception e){
System.out.println("sleep error!");
}
/*杀死线程*/
pro.destroy();
}
}
运行结果: