jdk源码
public class Runtime { private static Runtime currentRuntime = new Runtime(); /** * Returns the runtime object associated with the current Java application. * Most of the methods of class <code>Runtime</code> are instance * methods and must be invoked with respect to the current runtime object. * * @return the <code>Runtime</code> object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; } /** Don't let anyone else instantiate this class */ private Runtime() {} ... }
JDK 中,java.lang.Runtime 就是经典的单例模式(饿汉式)
单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对一些需要频繁创建销毁/耗时/耗费资源的对象(比如工具类、频繁访问数据库或文件的对象),单例模式可以提高系统性能。