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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:05:26+00:00 2026-06-17T20:05:26+00:00

Let me explain my software. What my software simply does is that it creates

  • 0

Let me explain my software. What my software simply does is that it creates 10 threads and assigns a number of tasks to each thread. Each thread then creates a Runtime Process that will start a cmd batch file which in turn will start a program that will telnet to a device (I have about 200 of them) to poll its configuration. Here is the code for my process creation:

Process p1 = java.lang.Runtime.getRuntime().exec("cmd /c  start /b /wait " + batchFile); 
int returnVal = p1.waitFor();

batchFile is the full path of the batch file. Don’t get me wrong, the software works fine up to 100% of the execution and it hanged ONLY ONE TIME at about 95%, so I’m trying to find a solution for that. Why it hanged is not my issue now, but rather how to deal with hangups later on..!

Now the problem is that I need to wait for the process to finish because my telnet client will write to a file that I will read later on in the thread; and hence the use of the .waitFor() . My question is how can I get the thread to understand that the external program hanged? In other words, can I give the external program some time limit to finish; and if it does not the thread will kill the process?

Also I have read about reading the error and output streams; however, I don’t think it is applicable here, or is it?

  • 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-17T20:05:27+00:00Added an answer on June 17, 2026 at 8:05 pm

    Also I have read about reading the error and output streams; however, I don’t think it is applicable here, or is it?

    Almost certainly yes, it is applicable. Try using ProcessBuilder instead of Runtime.exec and read all the output before calling waitFor. For example (exception handling omitted, in particular waitFor may throw InterruptedException before the process actually exited)

        ProcessBuilder pb = new ProcessBuilder(
            "cmd", "/c", "start", "/b", "/wait", batchFile);
        pb.redirectErrorStream(true);
        Process p = pb.start();
        IOUtils.closeQuietly(p.getOutputStream());
        IOUtils.copy(p.getInputStream(), System.out);
        // or alternatively throw away the output using
        // IOUtils.copy(p.getInputStream(), NullOutputStream.NULL_OUTPUT_STREAM);
        IOUtils.closeQuietly(p.getInputStream());
        int returnVal = p.waitFor();
    

    (IOUtils and NullOutputStream are from Apache commons-io).

    To answer the actual question in the title, if your process still hangs even after properly reading its output, you may need to use p.destroy() to forcibly terminate the process. You could define a timer task something like

        public class TimeoutProcessKiller extends TimerTask {
          private Process p;
          public TimeoutProcessKiller(Process p) {
            this.p = p;
          }
    
          public void run() {
            p.destroy();
          }
        }
    

    and then do

        // single shared timer instance
        Timer t = new Timer();
    
        // for each device
        Process p = pb.start();
        TimerTask killer = new TimeoutProcessKiller(p);
        t.schedule(killer, 30000);
        // ... read output stream as before ...
        int returnVal = p.waitFor();
        killer.cancel();
    

    This will cause the process to be killed if it has been running for more than 30 seconds (adjust the schedule call to change the timeout).

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

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
First let me explain. I have several addresses on the page that I put
Let me explain: There are some apps (like the Facebook app) that send emails
Let me explain what I mean in my title. Let's say, that for example
Let me explain the question first and then few of my solutions to the
Let me explain my situation. I have made a user control that contains an
Let me explain with an example. If n=4 and r=2 that means all 4
Let me explain more: we know that map function in jQuery acts as .Select()
Let me explain more detail. First of all; I know that if user press
Let me explain. I have a css page that is stored in a table,

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.