网上说得很清楚了:
Singleton链接
CmdInvokeSingleton.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public enum CmdInvokeSingleton { INSTANCE {
@Override protected void execShell(String shell) { try { Runtime rt = Runtime.getRuntime(); final Process process = rt.exec(shell); DaemonThread daemonThread = new DaemonThread(process); daemonThread.start(); int exitcode = process.waitFor(); daemonThread.interrupt(); System.out.println("finish:" + exitcode); } catch (Exception e) { e.printStackTrace(); } }
}; protected abstract void execShell(String shell); }
|
DaemonThread.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class DaemonThread extends Thread {
private Process process; public DaemonThread(Process process) { this.process = process; } public void run() { try { sleep(3000); } catch (InterruptedException e) { System.out.println(e.getMessage()); } process.destroy(); } }
|
调用的时候:
1 2 3 4 5 6 7
| try { String cmdString = "ls > out.txt"; CmdInvokeSingleton.INSTANCE.execShell(cmdString); } catch (Exception e) { e.printStackTrace(); return "invoke failed."; }
|
下一步可以学习cakephp。