开发者

Java: How to make this main thread wait for the new thread to terminate

开发者 https://www.devze.com 2022-12-30 01:36 出处:网络
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

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(i开发者_开发知识库ox);
            }
        }
    }).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();


You can join on that thread. You would get the instance of the thread and when needed to wait invoke its join method.

 Thread th = new Thread(new Runnable() {  ... } );
 th.start();
 //do work 
 //when need to wait for it to finish
 th.join();
 //th has now finished

Others will suggest a CountdownLatch, CyclicBarrier or even a Future but I find this to be easiest to implement on a very low level.


You have to assign your thread to a variable and later call join() on this variable.


final StringBuffer outtext = new StringBuffer("");  
Thread outputDrainThread = new Thread(new Runnable() {
    public void run() {
        // ... 
    }
}).start();

// ...

//  ***HERE IS WHERE I NEED TO WAIT FOR THE THREAD TO FINISH ***
outputDrainThread.join();    

// ...
return outtext.toString();
0

精彩评论

暂无评论...
验证码 换一张
取 消