Solved

How can I keep a ProcessBuilder Process from locking up when processing output streams?

Posted on 2010-11-29
12
1,294 Views
Last Modified: 2012-05-10
I've found numerous fixes posted for this issue online all stating that the processing must be done inside of a thread.  I setup the code to do this, but it still locks up.  BufferedReader.readLine() seems to be the root of the issue, whenever I'm stepping through the code in debug mode, things freeze up there.  The code is included below:

    public void method {
            ....
            proc = processBuilder.start();
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),"OUTPUT");
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),"ERROR");
            outputGobbler.start();
            errorGobbler.start();
            proc.destroy();
            ....
    }

    class StreamGobbler extends Thread {
        InputStream is;
        String type;

        StreamGobbler(InputStream is, String type){
            this.is = is;
            this.type = type;
        }

        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line = null;
                while((line = br.readLine()) != null){
                    System.out.println(type + ">" + line);
                }
                br.close();
            } catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }

Open in new window

0
Comment
Question by:FerrousHombre
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34233285
Are you sure it is not the proc.destroy() causing your problem.

Should you not be waiting for completion of your gobbler threads before destroying proc?
0
 

Author Comment

by:FerrousHombre
ID: 34233370
I've tried calling proc.waitFor() before the proc.destroy() call, but it doesn't seem to make any difference, the code just chugs and chugs.
0
 
LVL 92

Expert Comment

by:objects
ID: 34234076
>             proc.destroy();

you shouldn't be calling destroy()

> whenever I'm stepping through the code in debug mode, things freeze up there.

it will block there while it waits for output from the process, that is expected. It will block until the process completes.

The problem would seem more likely to be with the process you are running. It sounds like it does not complete.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:FerrousHombre
ID: 34234430
No, when I run the commands for that ProcessBuilder in the command line, they run very quickly.  Also, commenting out the proc.destroy() call didn't change anything.
0
 
LVL 92

Expert Comment

by:objects
ID: 34234484
running from the command line is not the same as running from exec()
whats the command?
0
 

Author Comment

by:FerrousHombre
ID: 34234632
It's just a .ksh script that returns the status of another process.

I realize that the command line is not the same as running from exec().  I mentioned that the command runs quickly there because you said it sounded like the command doesn't complete.  Also, I'm not running from exec(), I'm starting a Process via ProcessBuilder.start().
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34234659
What is the command when you run it from the command line
0
 
LVL 92

Expert Comment

by:objects
ID: 34234756
Be aware that exec()/ProcessBuilder do not use a shell and you need to manage the environment yourself
0
 

Author Comment

by:FerrousHombre
ID: 34235328
I did go through and make sure that the BufferedReader gets closed, the Process gets destroyed, etc.  I still have a circumstance where BufferedReader.readLine() causes the program to stop responding, though.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 34235339
>>BufferedReader.readLine() causes the program to stop responding, though.

That will block until more data are available to read
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 34235354
>  I still have a circumstance where BufferedReader.readLine() causes the program to stop responding, though.

As I mentioned earlier that will stop responding until the process completes. That is expected.

It is simply waiting for output from the process
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34285734
:)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
I'm a big fan of Windows' offline folder caching and have used it on my laptops for over a decade.  One thing I don't like about it, however, is how difficult Microsoft has made it for the cache to be moved out of the Windows folder.  Here's how to …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question