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

Runtime类和System类

//=================================================
// 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();
	}

}

 

System类是一些与系统相关属性和方法的集合,而且System类中所有的属性都是静态的,要想引用这些属性和方法,直接使用System类调用即可。

Runtime类和System类

//=================================================
// File Name       :	System_demo
//------------------------------------------------------------------------------
// Author          :	Common



//主类
//Function        : 	System_demo
public class System_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		long startTime = System.currentTimeMillis();	//取得开始计算之前的时间
		
		int sum = 0;					//累加操作
		for(int i=0;i<300000000;i++){
			sum += i;
		}
		
		long endTime = System.currentTimeMillis();	//取得开始计算之后的时间
		System.out.println("计算所花费的时间:"+(endTime-startTime)+"毫秒");
		
		System.getProperties().list(System.out);			//列出系统的全部属性
		
		System.out.println("系统版本为:"+System.getProperty("os.name")+System.getProperty("os.version")+System.getProperty("os.arch"));
		System.out.println("系统用户为:"+System.getProperty("user.name"));
		System.out.println("当前用户目录:"+System.getProperty("user.home"));
		System.out.println("当前用户工作目录:"+System.getProperty("user.dir"));
	}

}

 

垃圾对象的回收

System类中也有一个rc()方法,此方法也可以进行垃圾的收集,而且此方法实际上是对Runtime类中的gc()方法的封装,功能与其类似。

对一个对象进行回收,一个对象如果不再被任何栈内存所引用,那么此对象就可以被成为垃圾对象,等待被回收。实际上,等待的时间是不确定的,所以可以直接调用System.gc()方法进行垃圾的回收。

Runtime类和System类

 

System类对IO的支持

Runtime类和System类

 

<1>System.out

System.out是PrintStream的对象,在PrintStream中定义了一系列的print()和println()方法Runtime类和System类

Runtime类和System类

 

 

<2>System.err

System.err表示的是错误信息输出,如果程序出现错误,则可以直接使用System.err进行输出Runtime类和System类

 

 

<2>System.in

System.in实际上是一个键盘的输入流,其本身是InputStream类型的对象,可以利用System.in完成从键盘读取数据的功能。

指定空间的大小会出现空间限制,不指定大小则会在输入中文的时候产生乱码

Runtime类和System类

package System;

import java.io.InputStream;

//=================================================
// File Name       :	Systemin_demo
//------------------------------------------------------------------------------
// Author          :	Common





//主类
//Function        : 	Systemin_demo System;
public class Systemin_demo {

	public static void main(String[] args) throws Exception{
		// TODO 自动生成的方法存根
		InputStream input = System.in;			//从键盘接收数据
		byte b[] = new byte[1024];					//开辟空间,接收数据
		System.out.println("请输入内容:");
		int len = input.read(b);							//接收数据
		System.out.println("输入的内容:"+new String(b,0,len));
		input.close();
	}

}

 

输入/输出重定向

通过System类也可以改变System.in的输入流来源和System.out和System.err两个输出流的输出位置Runtime类和System类

相关文章:

  • 2022-12-23
  • 2022-01-09
  • 2022-01-11
  • 2021-09-02
  • 2021-12-01
  • 2022-12-23
猜你喜欢
  • 2021-11-07
  • 2021-09-28
  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
  • 2021-07-01
  • 2021-10-08
相关资源
相似解决方案