首页 > 代码库 > jdk 中Runtime之单例模式 学习
jdk 中Runtime之单例模式 学习
这段代码是我从源码中截取的,大家很容易看到currentRuntime是一个静态变量,getRunTime对应的就是getInstacne。
不是说这种方法不好吗?
1 public class Runtime { 2 45 private static Runtime currentRuntime = new Runtime(); 3 4 5 Returns the runtime object associated with the current Java application. Most of the methods of class Runtime are instance methods and must be invoked with respect to the current runtime object. 6 Returns: 7 the Runtime object associated with the current Java application. 8 54 9 55 public static Runtime getRuntime() {10 56 return currentRuntime;11 57 }12 13
1 package heelo; 2 class ExecDemo { 3 public static void main(String args[]){ 4 Runtime r = Runtime.getRuntime(); 5 Runtime r1=Runtime.getRuntime(); 6 if(r1==r) 7 { 8 System.out.println("他们是相同的"); 9 10 }11 Process p = null; 12 try{ 13 p = r.exec("notepad"); //打卡一个记事本程序。你可以再cmd敲个notepad试试14 } catch (Exception e) { 15 System.out.println("Error executing notepad."); 16 } 17 } 18 }
首先运行这代码,我们看到getRuntime获得的对象是相同的。同时exec可以执行其他进程。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。