簡體   English   中英

Java流程/ IO執行掛起

[英]Java Process/IO Execution Hang

這是我的工作流程:

我從數據庫獲得工作,運行一些任務,運行一個讀取文件並生成另一個文件的外部程序(通常需要10秒鍾)。 這是代碼:

Process p = Runtime.getRuntime().exec(prog, null, new File(path));

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

String s;
String errorString = "";
while((s = stdInput.readLine()) != null) {
    if(s.toLowerCase().contains("error")) {
        Log.writeLog("error: " + s);
        errorString += s + "\r\n";
    }
}

if(errorString.length() > 1) {
    Emailer.email(name + "*" + errorString, "ERROR");
}

while((s = stdError.readLine()) != null) {
    Log.writeLog("ERROR: " + s);
}

但是,該片段已停止。 我通過LogMeIn控制代碼在其上運行的服務器,登錄后,該進程將解除阻塞(總運行時間約為280秒)並繼續。 該過程沒有產生ERROR結果。 這種情況時有發生,比我們希望的更頻繁。 我們在程序中做了很多小的IO操作,硬盤驅動器有時會變得很滿。

知道會發生什么嗎?

謝謝!

編輯:服務器只是連接到LogMeIn的普通計算機。 我擔心的是,由於它是一台常規計算機,因此在不使用時可能會關閉CPU /硬盤驅動器的電源(不確定正確的術語)。 這在某種程度上解釋了為什么一旦我登錄LogMeIn並可以訪問計算機,它仍會繼續。

EDIT2:直接按照該過程運行。 而且這也掛了一段荒謬的時間(通常為5秒,花費了200多秒)。 讓我決定硬盤要打個?兒嗎?

private void cleanup(String path) {

    File srcPath = new File(path);
    File[] files = srcPath.listFiles();

    if(files != null) {
        for(File file : files) {
            if(file.isDirectory()) {
                cleanup(file.getAbsolutePath());
            } else {
                if(file.getAbsolutePath().endsWith(".original")) {
                    String fileName = file.getAbsolutePath().substring(0,     file.getAbsolutePath().lastIndexOf(".original"));
                    IO.delete(fileName);
                    if(!IO.renameFile(file.getAbsolutePath(), new File(fileName).getAbsolutePath())) {
                        Log.writeLogSevere("Failed to rename file, this could be a problem..." + fileName);
                    } else {
                        Log.writeLog("Cleaned up: " + fileName);
                    }
                }
            }
        }
    }
}

您不會耗盡錯誤流。 最后,您可能會做的太晚了。 進程的輸出緩沖區已滿,進程阻塞,等待在stderr輸出緩沖區中獲得更多空間。

為此,您必須使用單獨的線程,或者使用ProcessBuilder (簡單得多) redirectErrorStream

最有可能的事情是運行p的線程沒有死,並且p.getInputStream()不為null,但其中的數據也不為null。

掛起時,我將檢查當前正在運行的進程(Unix上為ps命令)或Windows上的任務管理器。 這將告訴您p是否完成。 如果不是,則無論該程序是什么,都將出現問題,並且將保留其余的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM