4 Replies Latest reply: Feb 18, 2007 1:30 PM by 807606 RSS

    Safe Thread Stopping

    807606
      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.
      private class ServerFeedbackWatcher implements 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;
              }
              
              public void safeToContinue(boolean shouldContinue)
              {
                  m_SafeToContinue = shouldContinue;
              }
      
              public void 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!");
              }
          }
        • 1. Re: Safe Thread Stopping
          807606
          You don't show the code where you start the thread, my guess is that you may have made the common mistake of calling run() on the thread object instead of start() (which runs the run() code in the Runable, but in the original thread).

          There can be problems, in any case, with halting IO operations. If the thread is in IO wait then the flag isn't going to be noticed. Try calling interrupt on the thread object, and failing that, close the reader you're thread is reading from.
          • 2. Re: Safe Thread Stopping
            807606
            I use this code to start my thread. m_Watcher is my created runnable class.
            m_FeedbackThread = new Thread(m_Watcher);
            m_FeedbackThread.start();
            The thread is meant to be an IO thread. I'm creating a serverr/client chat application. I got both the client and server working using consoles, but now I want to create GUIs for them. The thread used with my questions just sits there waiting for output from the server. If no output is given, nothing is done, otherwise the server's output is displayed in a text field of some sort. Atleast that is my goal.
            • 3. Re: Safe Thread Stopping
              807606
              The thing is - when the thread request input from the BufferedReader, it's made to wait until input is available. It doesn't return from the read, and hence it doesn't test the flag. You need something to kick it out of that wait.
              • 4. Re: Safe Thread Stopping
                807606
                Nope, if the BufferedReader is empty, null is returned. It does not sit there and wait the way System.In would do. I am unsure how, but I fixed my code. Thanks for your help :)