Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Threads

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hi,

I have a pretty simple question. I have written a multithreaded program and it seem to work pretty well. However, when the threads are done their execution, the program does not seem to finish, but just hangs. I thought that when the threads finished their execution, the resources were simply absorbed back to the system.

If I use a System.exit(0) call after the thread.start() call, the program immediately terminates which is not what I want.

Does anyone have any tips on how to proceed.


thanks
 
If your using any subsystems like RMI JINI etc you can have thread created by those libraries that are waiting for a timeout before they will exit.

So if you are then let us know about that. If not then you have some sort of a logic problem, you can use logging to know exactly when each thread starts and exits.

-pete

 
Thanks Pete,

I tried using logging. In other words, I put simply output statements when the threads are about to exit. They seem to get to the point as wanted, but they do not die after that.

I am not using RMI or JINI. I thought that threads were supposed to die upon their completion. I have tried setting the threads to null internally after they finish from within the thread, but I am not totally sure how to go about it. The statement: this=null makes no sense.

thanks
 
>> I put simply output statements when the threads are
>> about to exit.

Post that code.

-pete

 
public class FThread extends Thread {
private File inputFile;
private DataInputStream input; //input file stream
private String infoString;
private StringBuffer outputString;
private String inputName;

public FThread(String Name, String FName) {
super(Name);
outputString=new StringBuffer(10); // hold the output info.
inputName=new String(FName);
}


public void run()
{
// now process the list of URLs by reading them, getting info, finding results, and printing results to a file
try {
loadInfo(inputName); //loading the list of URLs into a String.
}
catch (IOException e)
{
System.out.println(e.toString());
}
processList();
System.out.println("\nFinishing Up....");
finishup(); //finish up with the files and write output to a file.
}


private void processList()
{
// the processing of the list of URLs is done here.
String part;
int start=0;
int pos1=0, pos2=0, pos3=0;
long positionOut=1;
while (pos1!=-1)
{
pos1=infoString.indexOf(".com", start);
if (pos1!=-1)
{
part=infoString.substring(start,pos1+4);
// got it System.out.println("\n\n"+part+"\n");
process(part); // ie process the URL
start=pos1+5; //ie. eliminate the new line character
positionOut++;
}
if ((positionOut%10000)==0)
System.out.println("Thread "+getName()+ "done 10000 lines");
}

}

private void process(String urlName)
{
// get the information
URL currURL;
String resultS1,resultS2;
String result="";

try
{
//currURL=new URL(" String temp=" currURL=new URL(temp);

HttpURLConnection conn=(HttpURLConnection)currURL.openConnection();
conn.connect();
//get an input stream from the URL connection object
InputStreamReader isr=new InputStreamReader(conn.getInputStream());
BufferedReader in =new BufferedReader(isr);
String hold;
while ((hold=in.readLine())!=null)
{
result+=hold+"\n";
}
in.close();
//now try processing this garbage
int ind1=result.indexOf(&quot;<tr bgcolor=#333333>&quot;,0);
int ind2;
if (ind1!=-1)
{
ind2=result.indexOf(&quot;</td>&quot;,ind1+83);
resultS1=result.substring(ind1+83,ind2);
// also write the result to a string that will be piped at the end to an outputFile.
outputString.append(urlName+&quot; &quot;+resultS1+&quot;\n&quot;);
in=null;
isr=null;
}

//String hold = currURL.getContent().toString();
//System.out.println(&quot;\ngot here&quot;);
// System.out.println(urlName);
// no need to replace anything
//System.out.println(&quot;\ngot here&quot;);
}

catch (ConnectException w)
{
System.out.println(&quot;Problem connecting with URL Info: &quot;+ urlName);
System.out.println(&quot;Retrying......&quot;);
process(urlName); //internal call.
}
catch (BindException b)
{
//ie. too many processes running simultaneously.
System.out.println(&quot;Problem connecting with URL Info: &quot;+ urlName);
System.out.println(&quot;Retrying....&quot;);
process(urlName); //internal call.
}
catch (Exception e)
{
System.out.println(&quot;Error. Retrying...&quot;);
System.out.println(e.toString());
process(urlName); //internal call
}
currURL=null; // clear up the memory

}


private void loadInfo(String FName) throws IOException
{
inputFile=new File(FName);

StringBuffer infoList;
try {
input=new DataInputStream(new FileInputStream(inputFile));
}
catch (IOException e) {
System.out.println(&quot;Error Opening File&quot;);
}

infoList=new StringBuffer(10);
char tmp;
try {
while(true) {
tmp=(char)input.readByte();
infoList.append(tmp);
}
}
catch (EOFException e) { }
System.out.println(&quot;Opened &quot;+FName);
//now set the text for the text area
infoString=new String(infoList.toString());
input.close();
}

public void finishup()
{
DataOutputStream output;
String fn=inputFile.getName();
int i = fn.indexOf(&quot;.txt&quot;);
String nn=fn.substring(0,i);
String outputName=nn+&quot;O.txt&quot;;
File outFile =new File (outputName);
String outputN=new String(outputString.toString());
//output the string to a file
try {
output=new DataOutputStream(new FileOutputStream(outputName));

for (int j=0; j<outputN.length(); j++)
{
output.writeByte(outputN.charAt(j));
}
output.close();
}
catch (IOException e) {
System.out.println(&quot;Error Opening Save File&quot;);
}
System.gc();
System.out.println(&quot;Thread &quot;+getName()+&quot; done...Exiting&quot;);
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top