?
Solved

slow

Posted on 2001-06-25
7
Medium Priority
?
248 Views
Last Modified: 2008-02-26
I am reading standard output from a child process using getInputStream. The standard output is empty. However, it spends a lot of time reading it (30-40 seconds). What is the problem and How do I fix it?
Thanks.
------------

Process p=Runtime.getRuntime().exec(cmd);
InputStream is = p.getInputStream();
int c;
while((c=is.read())!=-1)System.out.print((char)c);
......
0
Comment
Question by:menubar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 200 total points
ID: 6225133
Hi,

How much time takes process execution? I believe that is.read() will wait till precess out stream gets closed which happens typically on exit.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 

Author Comment

by:menubar
ID: 6225231
You are correct.
Can you also answer me this question?
The method "read()" is an abstract method inside the abstract class InputStream. API doc says the abstract method has to be implemented in a subclass.
However, I never sub-class InputStream. All I did is

InputStream is = p.getInputStream();
.. is.read()...

Who implemented "read()", how does this work?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6225420

Real class of the object returned from getInputStream() is not java.io.InputStream, but subclass which implements read() in some way--in this case in a way that makes program output available. Typically you don't need to subclass InputStream--there are plenty of subclasses to choose from. And you don't need to know precise class at compile time--what code will be executed in this case determined at runtime, because method is virtual (like most java instance methods).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:menubar
ID: 6225498
Sorry, I still dont understand.
getInputStream() is a method of Class Process. The doc says:
public abstract InputStream getInputStream()
The link of InputStream leads to java.io.InputStream.

>And you don't
need to know precise class at compile time--what code will be executed in this case determined at runtime,
because method is virtual (like most java instance methods).

Can you explain more? I'll up the points to 50. Thanks.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6225560
OK.

It's an example of polymorphism, one of the basic notions of OO programming. When in your code you have variable of base class type (InputStream), it may hold (and will hold, because InputStream is abstract class) a reference to an instance of subclass (e.g. FileInputStream). When you call read(), sequence of bytecodes to be executed determined by actual class of that instance (FileInputStream). Situation is same with function result. You need not know precise type of getInputStream() result. All you need to know is that is subclass of InputStream and must implement method read(). Try to add the following line somewhere after getInputStream() call:

System.out.println(is.getClass());

Does that make question more clear?

Regards,
Igor Bazarny

 
0
 

Author Comment

by:menubar
ID: 6227531
One last clafication:
what determines the instance of which subclass to be returned by getInputStream()? (Since InputStream has many subclasses, which I suppose can have different methods of their own.)
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6228566
getInputStream() implementation is free to choose which subclass to return.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 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