[問題]請問這樣的 Java Class 算不算是一個 Singleton !?
這個 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
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章