title: Kill and Clean a thread that calls a method?
body: <p>This is what I am trying to do:</p>
  
  <ul>
  <li>UI with start/stop button,</li>
  <li>when start/stop is clicked, start or stop a class method.</li>
  </ul>
  
  <p>This class method is a method that calls a <code>scheduleAtFixedRate</code> and also contains a while that checks for a condition. This loops until it found the condition.</p>
  
  <p>How can I make this whole process controlled by the user (in a nice and efficient way)?</p>
  
  <p>Optimally I want to do this:</p>
  
  <ul>
  <li><p>Start and call start Method</p></li>
  <li><p>Stop and call cleanup Method</p></li>
  </ul>
  
  <p>If I call the method directly from the UI, it freezes it (because of the while loop).</p>
  
  <p>Now, I tried calling the method from inside a new Thread's <code>run()</code> method, 
  and this works, but when stopping and calling the thread again, <code>IllegalThreadStateException</code> is thrown.</p>
  
  <p>I also tried <code>if (thread.isAlive()) { mainThread = new Thread(mainThread);}</code>
  But this also doesn't work (it throws the same error as before).</p>
  
  <p>What can I do? I am trying not to make the method that I call a thread/runnable, because it would fill half of it with try/catches (making the code pretty much unreadable).</p>
  
  <p>EDIT: 
  This is what I came up with. As said It doesn't work the second time.
  Missing variables are simple and can be inferred.</p>
  
  <h2>UI CODE:</h2>
  
  <pre><code>//Other code
   //Thread
  mainThread = new Thread(new Runnable() {
       @Override
       public void run() {
           try
                  {
                      magicalManager.execute(2);
                  }
              catch (Exception e){}
       }
  
  });
  
  
  
  ActionListener StartStopAL = new ActionListener()
      {
  
          public void actionPerformed(ActionEvent actionEvent)
              {
  
                  if (startButtonStatus == false) {
                      startButton.setText("Start");
                      startButtonStatus = true;
  
                              mainThread.interrupt();
                              TaskScheduler.getScheduledExecutorService().shutdown();
  
                  }
                  else if (startButtonStatus == true) {
                      startButton.setText("Stop");
                      startButtonStatus = false;
                      if (mainThread.isAlive()) {
                      mainThread = new Thread(mainThread);
                      }
                      mainThread.start();
  
                  }
  
              }
      };
  
  //Add action listener to startButton
  //Other code
  </code></pre>
  
  <h2>Class Code:</h2>
  
  <pre><code>public class magicalManager 
  {
      public static void execute(int ID) throws Exception {
          //run scheduleAtFixedRate
          while(...)
          {
              if (...){
              break;
              }
          }
          return
      }
      public static void KillManager(){
      //kill scheduleAtFixedRate
      }
  }
  </code></pre>