Java: How to make this main thread wait for the new thread to terminate
- by Jeff Bullard
I have a java class that creates a process, called child, using ProcessBuilder.  The child process generates a lot of output that I am draining on a separate thread to keep the main thread from getting blocked.  However, a little later on I need to wait for the output thread to complete/terminate before going on, and I'm not sure how to do that.  I think that join() is the usual way to do this but I'm not sure how to do that in this case.  Here is the relevant part of the java code.
    // Capture output from process called child on a separate thread
    final StringBuffer outtext = new StringBuffer("");
    new Thread(new Runnable() {
        public void run() {
            InputStream in = null;
            in = child.getInputStream();
            try {
                if (in != null) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String line = reader.readLine();
                    while ((line != null)) {
                        outtext.append(line).append("\n");
                        ServerFile.appendUserOpTextFile(userName, opname, outfile, line+"\n");
                        line = reader.readLine();
                    }
                }
            } catch (IOException iox) {
                throw new RuntimeException(iox);
            }
        }
    }).start();
    // Write input to for the child process on this main thread
    //
    String intext = ServerFile.readUserOpTextFile(userName, opname, infile);
    OutputStream out = child.getOutputStream();
    try {
        out.write(intext.getBytes());
        out.close();
    } catch (IOException iox) {
        throw new RuntimeException(iox);
    }
    //  ***HERE IS WHERE I NEED TO WAIT FOR THE THREAD TO FINISH ***
    // Other code goes here that needs to wait for outtext to get all
    // of the output from the process
    // Then, finally, when all the remaining code is finished, I return
    // the contents of outtext
    return outtext.toString();