[問題]請問這樣的 Java Class 算不算是一個 Singleton !?

看板Programming作者時間18年前 (2007/12/21 02:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
這個 Sample Code 的主要目地是讓 Java 去執行一個外部程式 但是同一個時間內只允許一個外部程式的執行 如果要執行第二個外部程式就必須等到前面的外部程式結束後才能執行 有人跟我說這不算是 Singleton 於是我就去翻 Design Patterns (四人幫那本) 看來看去還是搞不清楚是哪裡有問題!? 請版上的前輩指點小弟一下 <(_ _)> /* SingletonProcess.java */ import java.util.*; import java.io.*; public class SingletonProcess { public static void main(String args[]) { SingletonProcess.runCommand("dir"); SingletonProcess.waitForTermination(); } private static Process process = null; public static boolean runCommand(String cmd) { if (process != null) { System.err.println(" (*) Another process is running without termination."); return false; } try { process = Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); return false; } return true; } public static boolean waitForTermination() { if (process == null) { System.err.println(" (*) There is no process to wait for."); return false; } try { int exitVal = process.waitFor(); System.err.println(" (*) Process exitValue: " + exitVal); } catch (InterruptedException ie) { ie.printStackTrace(); } process = null; return true; } } // end of file -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: 59-104-128-80.adsl.dynamic.seed.net.tw
文章代碼(AID): #17QgtX00 (Programming)
文章代碼(AID): #17QgtX00 (Programming)