使用建議的方法有哪些瓶頸?
最大的問題是你的硬件和可執行文件本身。如果可執行文件在一段時間後自行完成,則應該沒問題。你不希望1000個進程在等待某些東西完成。您還需要在流程啓動後監控流程,並在流程超時時終止流程。另一個考慮是限制每個用戶的併發進程數量。
例如,如果用戶單擊按鈕並運行可執行文件,是否會影響第二個用戶的性能?
不直接。往上看。
或者如果他們同時按下按鈕,會發生什麼?
如果您每次都在新線程(或任務調度程序)上啓動進程,則什麼也不做。每按一次按鈕,一個新的過程開始。
或者我會如何知道執行是否完成(我不認爲OS會爲我管理)。
下面是一個例子:
String command = "PATH_TO_EXECUTABLE/EXECUTABLE PARAMS ";
log.info("Executing command - " + command);
// Executing the command
Process process;
ArrayList<String> outLines = new ArrayList<String>();
try {
process = Runtime.getRuntime().exec(command);
// Getting the results
process.getOutputStream().close();
String line;
log.info("Standard Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
process.getInputStream()));
while ((line = stdout.readLine()) != null) {
log.info(line);
outLines.add(line);
}
stdout.close();
log.info("Standard Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
while ((line = stderr.readLine()) != null) {
log.info(line);
}
stderr.close();
log.info("Done");
} catch (IOException e) {
log.error("Error while exceuting command", e);
}
String result = outLines.get(outLines.size()-1);
//TODO do something with the results
//at this point the process has finished executing
簡單地嘗試和配置文件的行爲。 – MrSmith42
創建一個線程並跟蹤外部進程的狀態。 –
看來,在實現如何運行可執行文件之前,您需要同步對按鈕的訪問。在考慮性能問題之前,您需要考慮程序的準確性。 – Juvanis