title: Restarting thread fails to pause
body: <p>I've got a tricky thread question for an Android app. I'm creating a Thread in <code>onCreate()</code> and interrupting it in <code>onDestroy()</code>:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private Handler mHandler;
      private Thread mThread;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mHandler = new Handler(Looper.getMainLooper()) {
              @Override
              public void handleMessage(Message inputMessage) {
                  Log.d("test", "Message: " + inputMessage.toString());
              }
          };
  
          mThread = new Thread(myRunnable);
          mThread.start();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          Log.d("test", "onDestroy");
          mThread.interrupt();
      }
  </code></pre>
  
  <p>The Runnable sends a message to the UI thread via the Handler <code>mHandler</code>, setting the message's <code>arg1</code> to a increasing integer. </p>
  
  <pre><code>    private Runnable myRunnable = new Runnable() {
          @Override
          public void run() {
  
              Log.d("test", "Start runnable");
  
              int count = 0;
              while (!Thread.interrupted()) {               
                  count++;
  
                  // Simulate a long task
                  for (int i = 0; i &lt; 100000000; i++) ;
  
                  // Tell UI thread the count
                  Message msg = mHandler.obtainMessage();
                  msg.arg1 = count;
                  mHandler.sendMessage(msg);    
              }
  
              Log.d("test", "Thread is interrupted");
          }
      };
  }
  </code></pre>
  
  <p>Logcat shows the Runnable starting, messages received by the UI thread with the increasing number (1, 2, etc.) that are spaced about a second apart, and the thread terminating when the activity is destroyed:</p>
  
  <pre><code>05-10 20:10:35.269 3236-3268/my.threadingdemo D/test: Start runnable
  05-10 20:10:37.200 3236-3236/my.threadingdemo D/test: Message: { when=0 what=0 arg1=1 }
  05-10 20:10:37.934 3236-3236/my.threadingdemo D/test: Message: { when=0 what=0 arg1=2 }
  05-10 20:10:38.694 3236-3236/my.threadingdemo D/test: Message: { when=-1ms what=0 arg1=3 }
  05-10 20:10:40.172 3236-3236/my.threadingdemo D/test: Message: { when=0 what=0 arg1=4 }
  05-10 20:10:40.394 3236-3236/my.threadingdemo D/test: onDestroy
  05-10 20:10:41.981 3236-3268/my.threadingdemo D/test: Thread is interrupted 
  </code></pre>
  
  <p>The problem is this: If I restart the app (press the Recents button and select the app or start it from the launcher), the Thread is restarted as expected, but Logcat gets flooded with messages. The for loop doesn't seem to be pausing the Runnable any more.  </p>
  
  <p>Here's a brief snippet from Logcat showing the first 7 messages are received all in the same second (20:10:48):</p>
  
  <pre><code>05-10 20:10:48.280 3236-3308/my.threadingdemo D/test: Start runnable
  05-10 20:10:48.315 3236-3236/my.threadingdemo D/test: Message: { when=-33ms what=0 arg1=1 }
  05-10 20:10:48.318 3236-3236/my.threadingdemo D/test: Message: { when=-38ms what=0 arg1=2 }
  05-10 20:10:48.319 3236-3236/my.threadingdemo D/test: Message: { when=-39ms what=0 arg1=3 }
  05-10 20:10:48.320 3236-3236/my.threadingdemo D/test: Message: { when=-39ms what=0 arg1=4 }
  05-10 20:10:48.870 3236-3236/my.threadingdemo D/test: Message: { when=-584ms what=0 arg1=5 }
  05-10 20:10:48.871 3236-3236/my.threadingdemo D/test: Message: { when=-585ms what=0 arg1=6 }
  05-10 20:10:48.872 3236-3236/my.threadingdemo D/test: Message: { when=-586ms what=0 arg1=7 }
  </code></pre>
  
  <p>I don't understand why the for loop appears to be unable to slow the messages down.  Replacing the for loop with <code>Thread.sleep(1000)</code> will cause a 1 second delay between messages, but shouldn't the for loop be adequate?  It's weird that the for loop slows things down the first time the app is started but not afterwords.</p>