title: android: how to stop and start a thread (with buggy code example)
body: <p>I tried to create a thread in app. Here is the code:</p>
  
  <pre><code>private class GetFileTask extends Thread {
      private volatile boolean mKeepRunning = false;
      public GetFileTask() {
          super("GetFileTask");         
      }
      /** Cancel task */
      public void cancel() {
          mKeepRunning = false;
          interrupt();
      }
      @Override
      public void run() {
          mKeepRunning = true;
          while (true) {
              try {
                doTask();
                Sleep(300);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              if (!mKeepRunning) {
                  break;
              }
          }
      }
      private void doTask() {
      }    
  }
  </code></pre>
  
  <p>I tried to let the user to cancel download (cancel the thread), and later on, start to download from zero again ( restart the thread). I read android sdk doc, and I know I should not use pause/suspend. I tried to cancel the thread by calling <code>cancel();</code> and tried to restart the thread by calling <code>run();</code>
  However, my app hang when I called <code>run();</code></p>
  
  <p>So, based on above code sample, what is the correct way to <code>stop</code> and <code>restart</code> a thread? </p>