单例模式-案例Runtime

package d.create_type_single;

import java.io.IOException;

/**
 * Runtime类就是使用的单例:并且是饿汉式
 * (原因考虑是因为:多线程)
 * 
 * @author Administrator
 * 
 *  Process exec(String command)   在单独的进程中执行指定的字符串命令。 
 */
public class RuntimeDemo {
	public static void main(String[] args) throws IOException {
		Runtime r = Runtime.getRuntime();
		// 在进程中使用命令行,此方法打开记事本
		// 父进程打开子进程
		r.exec("notepad");
		// 启动关机的命令行,离关机还有10000秒
		r.exec("shutdown -s -t 10000");
		// 取消关机命令
		r.exec("shutdown -a");
	}
}

/* 在多线程中我们一般使用饿汉式!
 * class Runtime { 
 * 		private Runtime() {}
 * 		private static Runtime currentRuntime = new Runtime();
 * 		public static Runtime getRuntime() {
 * 			return currentRuntime;
 * 		} 
 * }
 */


猜你喜欢

转载自blog.csdn.net/qq_36791569/article/details/80369643
今日推荐