Safe Thread Stopping
I don't quite understand threads and I have a few questions. Right now I am using two classes to make only on thread. It feels wrong to me but I am unsure how to stop my thread any other way (well, actually my way doesn't stop the thread), I started with making a class that implements Runnable and I would create a thread using a Thread t = new Thread(MyRunnableClass) type call. Noticing depreciation of the stop() method, I started using a boolean flag to halt my execution of code. I knew this would work to stop the execution, but for some reason it doesn't stop the thread. Why is that? My runnable class is posted below. Thanks for the help.
privateclass ServerFeedbackWatcherimplements Runnable
{
BufferedReader m_DataToRead;
JTextComponent m_TextToUpdate;
boolean m_SafeToContinue;
public ServerFeedbackWatcher(BufferedReader readFromMe,
JTextComponent updateMe)
{
m_DataToRead = readFromMe;
m_TextToUpdate = updateMe;
m_SafeToContinue =true;
}
publicvoid safeToContinue(boolean shouldContinue)
{
m_SafeToContinue = shouldContinue;
}
publicvoid run()
{
System.out.println("Hello from a thread!");
String fromServer =new String();
while(m_DataToRead!=null&&m_SafeToContinue)
{
try
{
fromServer = m_DataToRead.readLine();
}
catch (IOException ex)
{
m_Error +="Failed to read from server connection.\n";
}
if(fromServer!=null&&m_SafeToContinue)
{
String previousText = m_TextToUpdate.getText();
String newText ="Server: " + fromServer +"\n";
String updatedText = previousText + newText;
m_TextToUpdate.setText(updatedText);
}
}
System.out.println("Goodbye from a thread!");
}
}

