Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 775809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:21:03+00:00 2026-05-14T19:21:03+00:00

I have a java class that creates a process, called child, using ProcessBuilder. The

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:21:04+00:00Added an answer on May 14, 2026 at 7:21 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you actually want to alert the index: Example: http://jsfiddle.net/Ksyr6/… May 17, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer Since you can't use a table parameter, try passing in… May 17, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer data load options always have to be set before execution… May 17, 2026 at 12:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In Java i have abstract class named Operation and three its subclasses called OperationActivation,
I use Java. I already have a class for a custom object called Subject.
I'm creating a Java app that creates 4 PDF files using iText. On the
I'm battling an error I don't quite understand. I have a class in Java,
I have just started with Gui Programming in netbeans (Using the template Java Desktop
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL
Following on from my recent question regarding parsing XML files in Java I have
I am in the process of converting some code from C# to Java. I
I have a project in Java requiring to load large files (corpus for NLP)
Coming to Python from Java, I've been told that factories are not Pythonic. Thus,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.