Article View: borland.public.jbuilder.java.language
Article #20124Can anyone in the world find a bug in this code
From: Johnathan Mark Smith
Date: Fri, 24 Oct 1997 00:00
Date: Fri, 24 Oct 1997 00:00
235 lines
6909 bytes
6909 bytes
Can anyone in the world find a bug in this code
Please following source code works great on NT/WIN95, but on HP 10.20
UNIX the code will hang the VM. Please take a look and see if you can
help out. PLEASE EMAIL BACK ANY HELP
public class TestjProcesser
{
public static void main(String[] arg)
{
try
{
Runtime.getRuntime().gc();
Process p = Runtime.getRuntime().exec(arg);
Process p2 = Runtime.getRuntime().exec(arg);
Thread myProcesser = new Thread(new jProcesser(p,true));
Thread myProcesser2 = new Thread(new jProcesser(p2,
true));
myProcesser.start();
myProcesser2.start();
while(myProcesser.isAlive())
{
}
while(myProcesser2.isAlive());
System.out.println("Return Code: " + p.exitValue());
}
catch(Exception e)
{
System.out.println("Error" + e);
}
System.out.println("It Works");
}
}
import java.util.*;
import java.io.*;
/**
* jProcesser will make it easy for a programmer to display the output
stream and the error stream or a process
* <P>
*
* Example for an non-applet:
* <pre>
* try
* {
* Process p = Runtime.getRuntime().exec(args[0]);
*
* Thread myProcesser = new Thread(new jProcesser(p));
* myProcesser.start();
* while(myProcesser.isAlive());
* System.out.println("Return Code: " + p.exitValue());
*
* } catch(Exception e)
* {
* System.out.println("Error" + e);
* }
*
* </pre>
*
* @author Johnathan Mark Smith (smithj@statenislandonline.com)
* @version 1.0 10/16/97
*
*/
public final class jProcesser implements Runnable
{
private boolean rundown = false;
private boolean errorStream = false;
boolean isProcess = false;
boolean waitForProcess = true;
boolean printout = false;
String printString = "";
Process myp;
private jProcesser pProcess = null;
DataInputStream myStream;
/**
* jProcesser will connnect to the process you pass in as a param
*
* @param inProcess The process to connect to.
*/
public jProcesser (Process inProcess)
{
myp = inProcess;
isProcess = true;
}
/**
* jProcesser will connnect to the process you pass in as a param
*
* @param inProcess The process to connect to.
* @param useStandedout to display the output of your process to
standout
*/
public jProcesser(Process inProcess, boolean useStandedout)
{
myp = inProcess;
isProcess = true;
printout = useStandedout;
}
private jProcesser (DataInputStream inStream, jProcesser inP,
boolean inErrorStream, boolean useStandedout)
{
myStream = inStream;
pProcess = inP;
isProcess = false;
this.errorStream = inErrorStream;
printout = useStandedout;
}
/**
* run will start the thread. You have to execute this method !
*
*/
public synchronized void run()
{
if (isProcess)
{
Thread us = Thread.currentThread();
try
{
DataInputStream output = new DataInputStream(new
BufferedInputStream(myp.getInputStream()));
DataInputStream error = new DataInputStream(new
BufferedInputStream(myp.getErrorStream()));
Thread outputThread = new Thread(new
jProcesser(output, this, false, printout));
outputThread.start();
Thread errorThread = new Thread(new jProcesser(error,
this, true, printout));
errorThread.start();
System.out.println();
System.out.flush();
us.yield(); // give worker threads a chance!
myp.waitFor();
us.sleep(500);
us.yield();
while(error.available() > 0 && output.available() > 0)
{
us.sleep(2000);
}
rundown = true;
outputThread.join();
errorThread.join();
System.out.flush();
}
catch (Exception e)
{
System.out.println(e);
}
} // End of if (isProcess)
else
{
Thread us = Thread.currentThread();
int i;
try
{
while(!pProcess.rundown)
{
if (myStream.available() > 0)
{
while ((i = myStream.read()) != -1)
{
if(printout)
{
printString = printString +
(char)i;
if ((char)i == '\n')
{
if (errorStream)
{
System.err.print(printString);
System.err.flush();
}
else
{
System.out.print(printString);
System.out.flush();
}
printString = "";
}
}
us.sleep(100);
}
}
}
} catch (Exception e)
{
System.out.println(e);
}
} // End of else for if (isProcess)
}
}
* - Free advice is easily worth twice what you pay for it - *
Staten Island Java Group
http://www.statenislandonline.com/java
Technical Details
Message-ID:
<34509afe.64568544@news.tiac.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,9acf885d1ba81a8d,start X-Google-Attributes: gidf74b3,public From: smithj@statenislandonline.com (Johnathan Mark Smith) Subject: Can anyone in the world find a bug in this code Date: 1997/10/24 Message-ID: <34509afe.64568544@news.tiac.net> X-Deja-AN: 284747325 Organization: The Internet Access Company, Inc. Newsgroups: borland.public.jbuilder.java.language