很久没发帖了,请教一下Runtime本身的设计问题
知道Runtime获取对象需要用getRuntime而不是用new,用了单例模式,可是当初的设计者为什么把他设计成这样子?
Runtime属于单例类,采用单例模式,不能new,只能通过方法获取对象。
单例类最重要的特点是构造方法私有化,这样在类得外部就不能构造和创建类得实例。
下面是Runtime类的源码
通过源码,我们就可以看出它确实是单例类。
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;
}
……
}
[解决办法]
出发点是保证同一时刻只有一个exec执行。
[解决办法]
一个正在运行的JRE只有一个运行时Runtime
[解决办法]
噢,原来如此,如果是new的话就有多个Runtime了