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 7740479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:47:21+00:00 2026-06-01T08:47:21+00:00

So I’ve got a Java class that implements Runnable. In the run method I’ve

  • 0

So I’ve got a Java class that implements Runnable. In the run method I’ve got the following code and I don’t fully understand why it works the way it seems to and was hoping someone could explain it to me.

The code is:

public void run() {
    Process p = null;
    ProcessBuilder builder = new ProcessBuilder();
    builder.redirectErrorStream(true);

    p = builder.command("/opt/program/testscript.sh").start();

    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;

    try {
    while ((line = br.readLine()) != null) {
        log.info(line);
    }

    boolean running = true;
    while(running) {
        p.waitFor();
        running = false;
    }       

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
        p.destroy();
    }
}

So, the top part is getting the output from the process that’s running and displaying it. The middle part though seems to be where the code waits for the thread to process. As I understand it, the code should stop and wait in the while loop at the p.waitFor() command until the thread finishes, and then the whole thing should end. However, output continues to be displayed while the thread is running so obviously the while loop at the top is still being hit.

Threading has always been my weakest point and clearly I’m just not totally grasping what’s going on here. The code works though so I guess it’s valid. Though perhaps it’s not coded the way it should be? Anyway, help is much appreciated, thanks.

UPDATE

Thanks for the info all. Makes sense that the while(running) loop doesn’t do anything. I will remove it. I added the rest of the code up above that I didn’t provide before to give some more context in case anyone has anything further to add.

  • 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-06-01T08:47:22+00:00Added an answer on June 1, 2026 at 8:47 am

    The first loop stops when who ever is filling br closes it.

    p.waitFor() indeed waits until p finishes. The loop is kind of void since it executes exactly once, so you might as well remove it.

    Sooo … assuming p is whats filling up br until it is done and exits, the waitFor will only have to wait a very short time, possibly not at all.

    In order to understand more exactly what is going on one would need the code behind p, of whatever fills br and how these things get started.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.