---
title: "采用单例设计模式的Runtime类"
author: "yanln"
date: "2017-03-23T13:42:12Z"
tags: []
categories: ["编程语言"]
slug: "runtime"
draft: false
lastmod: "2017-03-23T13:42:12Z"
id: 2365003
url: "https://yanln.iteye.com/blog/2365003"
---

Runtime类属于java.lang包中的类，Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。

 

Runtime类的设计采用的是单例设计模式(饿汉式)：

![](http://dl2.iteye.com/upload/attachment/0123/8708/5fbc94ef-786c-3bfb-9fe5-cc0cd781cdcd.png)

 

所以想得到当前运行的Runtime对象，需要通过 getRuntime 方法获取。比如：

```
import java.io.IOException;
public class Test {	
	public static void main(String[] args)  {
		Runtime r = Runtime.getRuntime();		
		try {
			r.exec("notepad"); //在单独的进程中执行指定的字符串命令
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}
}
```