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();
    	}
    
    }
    
  • 相关阅读:
    git revert和git reset的区别
    git merge 和 git rebase 小结
    Markdown——入门指南
    使用Git Wiki 管理文档时,文档编写的基本用法
    mysql 列 默认添加 dedault null ??
    redis aof文件过大问题
    redis源码分析——aofrewrite
    改变文章的字号大小
    推荐大家使用的CSS书写规范及顺序
    inline-block 前世今生
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5263691.html
Copyright © 2011-2022 走看看