程式可鎖定系統資源或檔案來防止重複執行,例如 socket port,可參考:
https://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java
例如,該文所述:
private static boolean available(int port) {
try (Socket ignored = new Socket("localhost", port)) {
return false;
} catch (IOException ignored) {
return true;
}
}