java subprocess does not write its output until it

2019-09-10 08:04发布

问题:

I've written this simple testcase that describes the problem I'm having:

I create a subprocess from Java, and simultaneously start a thread that SHOULD write every line as soon as it is read from subprocess' standard output.

What I get instead is that the subprocess' output is entirely written when it terminates. Here is the output:

Mon Jul 15 19:17:13 CEST 2013: starting process
Mon Jul 15 19:17:14 CEST 2013: process started
Mon Jul 15 19:17:14 CEST 2013: waiting for process termination
Mon Jul 15 19:17:14 CEST 2013: readerThread is starting
Mon Jul 15 19:17:19 CEST 2013: process terminated correctly
Mon Jul 15 19:17:19 CEST 2013: Thread[Thread-0,5,main] got line: foo(7)
Mon Jul 15 19:17:19 CEST 2013: Thread[Thread-0,5,main] got line: foo(49)
Mon Jul 15 19:17:19 CEST 2013: Thread[Thread-0,5,main] got line: foo(73)
Mon Jul 15 19:17:19 CEST 2013: Thread[Thread-0,5,main] got line: foo(58)
Mon Jul 15 19:17:19 CEST 2013: Thread[Thread-0,5,main] got line: foo(30)
Mon Jul 15 19:17:19 CEST 2013: readerThread is terminating

with this code:

public class MiniTest {
    static void println(String x) {
        System.out.println(new Date() + ": " + x);
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder("bin/dummy", "foo", "5");

        println("starting process");
        Process p = pb.start();
        println("process started");

        new ReaderThread(p).start();

        println("waiting for process termination");
        p.waitFor();
        println("process terminated correctly");
    }

    static class ReaderThread extends Thread {
        private Process p;

        public ReaderThread(Process p) {
            this.p = p;
        }

        public void run() {
            println("readerThread is starting");
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            try {
                while((line = r.readLine()) != null) {
                    println(this + " got line: " + line);
                }
            } catch(IOException e) {
                println("read error: " + e);
            }
            println("readerThread is terminating");
        }
    }
}

Note: the subprocess is very simple, it outputs a line every second, for a specified abount of iterations (and when tested on the command line, it does so):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    char *f = argv[1];
    int n = atoi(argv[2]);
    while(n-- > 0) {
        printf("%s(%d)\n", f, rand() % 100);
        sleep(1);
    }
    return 0;
}

回答1:

Found the answer by asking around and studying on the issue.

The problem cannot be easily solved, but apparently it's a libc issue. If the output it's not a tty/pty, the buffering is not by line.

There are external programs or script however that allow to work around this, like for example stdbuf or unbuffer (from expect).

Detailed explanation: Force line-buffering of stdout when piping to tee