简体   繁体   English

Java流程/ IO执行挂起

[英]Java Process/IO Execution Hang

Here is my workflow: 这是我的工作流程:

I get job from DB, I run a few tasks, I run an external program that reads a file and produces another one (this usually takes under 10 seconds). 我从数据库获得工作,运行一些任务,运行一个读取文件并生成另一个文件的外部程序(通常需要10秒钟)。 Here is the code: 这是代码:

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);
}

However, the snippet hanged. 但是,该片段已停止。 I control the server that the code runs on through LogMeIn, once I logged in, the process unblocked (total running time around 280 seconds) and continued. 我通过LogMeIn控制代码在其上运行的服务器,登录后,该进程将解除阻塞(总运行时间约为280秒)并继续。 The process did not produce an ERROR results. 该过程没有产生ERROR结果。 This happens from time to time more often than we would like to. 这种情况时有发生,比我们希望的更频繁。 We do quite a bit of small IOs operation in the program and the harddrive gets pretty full from time to time. 我们在程序中做了很多小的IO操作,硬盘驱动器有时会变得很满。

Any idea what could be happening? 知道会发生什么吗?

Thanks! 谢谢!

EDIT: the server is a just a regular computer that is connected to LogMeIn. 编辑:服务器只是连接到LogMeIn的普通计算机。 My fear is that since it is a regular computer, it may powerdown the CPU/hard drive when not in use (not sure the correct terminology). 我担心的是,由于它是一台常规计算机,因此在不使用时可能会关闭CPU /硬盘驱动器的电源(不确定正确的术语)。 This would somewhat explain why it would continue once I logged in to LogMeIn and had access to a computer. 这在某种程度上解释了为什么一旦我登录LogMeIn并可以访问计算机,它仍会继续。

EDIT2: directly following the process, I run this. EDIT2:直接按照该过程运行。 And this hangs for an absurd amount of time as well (usually 5 seconds, took 200+ seconds). 而且这也挂了一段荒谬的时间(通常为5秒,花费了200多秒)。 Makes me thing that the hard drive is decided to take a nap? 让我决定硬盘要打个?儿吗?

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);
                    }
                }
            }
        }
    }
}

You are not draining the error stream. 您不会耗尽错误流。 You do it at the end, which may often be too late. 最后,您可能会做的太晚了。 The output buffer of the process fills up and the process blocks waiting to get more space in the stderr output buffer. 进程的输出缓冲区已满,进程阻塞,等待在stderr输出缓冲区中获得更多空间。

You must either use a separate thread for that, or (much simpler) redirectErrorStream using ProcessBuilder . 为此,您必须使用单独的线程,或者使用ProcessBuilder (简单得多) redirectErrorStream

The most likely thing is that the thread running p didn't die and p.getInputStream() is not null, and but not data on it. 最有可能的事情是运行p的线程没有死,并且p.getInputStream()不为null,但其中的数据也不为null。

While it's hanging, I would check the current running processes ( ps command on Unix) or Task manager on windows. 挂起时,我将检查当前正在运行的进程(Unix上为ps命令)或Windows上的任务管理器。 This will tell you if p is done or not. 这将告诉您p是否完成。 If it is not, then whatever that program is, has issues and it's holding up the rest of your code. 如果不是,则无论该程序是什么,都将出现问题,并且将保留其余的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM