zoukankan      html  css  js  c++  java
  • Runtime类

    Runtime类表示运行时的操作类,是一个封装了JVM进程的类,每一个JVM都对应着一个Runtime类的实例,此实例由JVM运行时为其实例化。

    //=================================================
    // File Name       :	StringBuffer_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    
    
    //主类
    //Function        : 	StringBuffer_demo
    public class Runtime_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		
    		Runtime run = Runtime.getRuntime();
    		
    		System.out.println("JVM最大内存:"+run.maxMemory());
    		System.out.println("JVM空闲内存量:"+run.freeMemory());
    		String str = "HELLO";
    		for (int i=0;i<1000;i++){
    			System.out.println(str += i);
    		}
    		System.out.println("JVM空闲内存量:"+run.freeMemory());
    		run.gc();					//进行垃圾收集,释放空间
    		System.out.println("JVM空闲内存量:"+run.freeMemory());
    	}
    
    }
    

    Runtime类与Process类

    可以直接使用Runtime类运行本机的可执行程序

    //=================================================
    // File Name       :	StringBuffer_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    
    
    //主类
    //Function        : 	StringBuffer_demo
    public class Runtime_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		
    		Runtime run = Runtime.getRuntime();
    	
    		try{
    			run.exec("notepad.exe");
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    
    }
    

    可以通过控制Process进行系统的进程控制,如果想要让进程消失,则可以使用destroy()方法

    //=================================================
    // File Name       :	StringBuffer_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    
    
    //主类
    //Function        : 	StringBuffer_demo
    public class Runtime_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		
    		Runtime run = Runtime.getRuntime();
    	
    		Process pro = null;		//声明一个Process对象,接收启动的进程
    		
    		try{
    			pro = run.exec("notepad.exe");
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		try{
    			Thread.sleep(5000);
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		pro.destroy();
    	}
    
    }
    
  • 相关阅读:
    Servlet访问第一次500,刷新后404的解决办法
    关于eclipse保存代码很慢,提示the user operation is waiting的问题
    编译时,运行时解释
    JDK、JRE、JVM
    IDEA使用maven中tomcat插件启动项目乱码问题
    treeGrid树形数据表格的json数据格式说明
    Maven最佳实践:Maven仓库(转)
    intelliJ idea debug模式下启动慢的原因
    基于 Annotation 拦截的 Spring AOP 权限验证方法
    化繁为简 如何向老婆解释MapReduce?(转载)
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5263691.html
Copyright © 2011-2022 走看看