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();
    	}
    
    }
    
  • 相关阅读:
    异步编程:利用委托实现的.NET历史上第一个异步编程模型
    C#:委托
    C#:方法参数之--扩展方法
    py实现ftp
    sftp接口机上传脚本
    通过telnet自动下载cfg配置文件
    下载bat脚本
    solairs11与solairs10 ftp服务的区别
    windows中ftp下载脚本(bat+vb)
    windows2008r2防火墙设置一例
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5263691.html
Copyright © 2011-2022 走看看