emus
06-05-2003, 07:06 PM
How can I return data from a thread?
public class Reader extends Thread
{
String finalStr;
public void run()
{
until EOF
read data from file and append
to string buffers
finalStr=stringBuffer.toString();
}
public String finalize()
{
return(finalStr);
}
}
If I call System.out.println(finalize()); from my main class, I won't get the desired output since the Thread won't have finished. How can I ensure that the thread finishes before the result is printed out? If I tell the thread to yield after starting it, it works sometimes, but not always.
Does anyone know the correct method of returning data from a trhead?
Thanks,
emus
public class Reader extends Thread
{
String finalStr;
public void run()
{
until EOF
read data from file and append
to string buffers
finalStr=stringBuffer.toString();
}
public String finalize()
{
return(finalStr);
}
}
If I call System.out.println(finalize()); from my main class, I won't get the desired output since the Thread won't have finished. How can I ensure that the thread finishes before the result is printed out? If I tell the thread to yield after starting it, it works sometimes, but not always.
Does anyone know the correct method of returning data from a trhead?
Thanks,
emus