title: Updating UI from different thread
body: <p>Can anyone explain when I put the thread to sleep for 30 milliseconds it updates the UI from the background thread. but when I put the sleep for 300 milliseconds it crashes saying that you cannot touch view from another thread. I expect it to crash in both cases but somehow it is working in 30 milliseconds. Needs some clarification on this issue.</p>
  
  <pre><code>public class HandlerDemo extends Activity implements Handler.Callback{
  
  private static final String TAG = "FFFF";
  private Handler mHandler = null;
  private Handler mUIHandler = null;
  private HandlerThread backgroundThread = null;
  
  public static final int BACKGROUND_OPERATION = 10;
  public static final int MAIN_THREAD_OPERATION = 20;
  
  private TextView asd;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      HandlerThread backgroundThread = new HandlerThread("BACKGROUND_THREAD");
      backgroundThread.start();
      mHandler = new Handler(backgroundThread.getLooper(), this);
      mHandler.sendEmptyMessage(BACKGROUND_OPERATION);
  
      //        mUIHandler = new Handler(Looper.getMainLooper(), this);
      //        mUIHandler.sendEmptyMessage(MAIN_THREAD_OPERATION);
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      mHandler.getLooper().quit();
  }
  
  @Override
  public boolean handleMessage(Message message) {
      Log.d(TAG, Thread.currentThread() + "");
      switch (message.what) {
          case MAIN_THREAD_OPERATION:
              try {
                  Thread.sleep(30000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              ((TextView) findViewById(R.id.textview)).setText("Updating from UI Handler");
              findViewById(R.id.textview).invalidate();
              break;
          case BACKGROUND_OPERATION:
              try {
                  **Thread.sleep(300);**
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              ((TextView) findViewById(R.id.textview)).setText("Updating from background Handler");
              findViewById(R.id.textview).invalidate();
              break;
          default:
              break;
      }
  
      //        message.recycle();
      return true;
      }
  }
  </code></pre>