title: Example communicating with HandlerThread
body: <p>I want to set up a HandlerThread from the GUI thread. Then some time later, when a button is clicked on the GUI, it runs callHello(), which then send a message to a HelloLogger object residing on the non-GUI thread which asynchronously logs "Hello World". I have tried a number of things, some block indefinitely, some never receive the message, etc etc. The code below is more or less as close as I have got, please could someone modify it to work? </p>
  
  <pre><code>public class HandlerThreadExample {
  
      private MyHandlerThread mMyHandlerThread;
      private Looper mLooper;
      private Handler mHandler;
  
      public HandlerThreadExample(){
          mMyHandlerThread = new MyHandlerThread();
          mMyHandlerThread.start();
          mLooper = mMyHandlerThread.getLooper();
      }
      public void callHello() {
          mHandler.sendEmptyMessage(1);
      }
      private class MyHandlerThread extends HandlerThread {
          private HelloLogger mHelloLogger;
          private Handler mHandler;
          public MyHandlerThread() {
              super("The MyHandlerThread thread", HandlerThread.NORM_PRIORITY);
          }
          public void run (){
              mHelloLogger = new HelloLogger();
              mHandler = new Handler(getLooper()){
                  public void handleMessage(Message msg){
                      mHelloLogger.logHello();
                  }
              };
              super.run();
          }
      }
      private class HelloLogger {
          public HelloLogger (){
          }
          public void logHello(){
              Log.d("HandlerThreadExample", "Hello World");
          }
      }
  }
  </code></pre>
  
  <p>Best examples found:</p>
  
  <ul>
  <li><a href="http://alvinalexander.com/java/jwarehouse/android/core/tests/coretests/src/android/os/HandlerThreadTest.java.shtml%20%22HandlerThread%20test" rel="noreferrer">HandlerThread Test</a></li>
  <li><a href="https://stackoverflow.com/questions/4838207/how-to-create-a-looper-thread-then-send-it-a-message-immediately">How to create a Looper thread, then send it a message immediately?</a></li>
  <li><a href="http://mobile.awsblog.com/post/TxQ1NHASJ41XL9/Making-Asynchronous-Calls-with-Handler" rel="noreferrer">Async calls with Handler</a></li>
  <li><a href="https://stackoverflow.com/questions/7462098/handlerthread-vs-executor-when-is-one-more-appropriate-over-the-other">HandlerThread vs Executor - When is one more appropriate over the other?</a></li>
  <li><a href="https://stackoverflow.com/questions/18149964/best-use-of-handlerthread-over-other-similar-classes">Best use of HandlerThread over other similar classes</a></li>
  <li><a href="http://stephendnicholas.com/archives/42" rel="noreferrer">Android HandlerThread</a></li>
  <li><a href="http://www.programcreek.com/java-api-examples/index.php?api=android.os.HandlerThread" rel="noreferrer">HandlerThread examples</a></li>
  <li><a href="http://techtej.blogspot.co.uk/2011/02/android-passing-data-between-main.html" rel="noreferrer">Android: Passing data between main and worker threads</a></li>
  <li><a href="http://tutorials.jenkov.com/java-concurrency/synchronized.html" rel="noreferrer">Java Synchronised</a></li>
  <li><a href="http://androidcookbook.com/Recipe.seam?recipeId=921&amp;title=Sending%20messages%20between%20threads%20using%20activity%20thread%20queue%20and%20Handler%20class" rel="noreferrer">Sending messages between threads using activity thread queue and Handler class</a></li>
  <li><a href="http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/" rel="noreferrer">Intro to Loopers and Handlers</a></li>
  <li><a href="https://developer.android.com/training/multiple-threads/define-runnable.html" rel="noreferrer">developer.android: Specifying the Code to Run on a Thread</a></li>
  </ul>
  
  <p>At least now I can close the damned tabs</p>
  
  <p><strong>Solution courtesy of help from pskink</strong></p>
  
  <pre><code>public class HandlerThreadExample2 {
      private static int MSG_START_HELLO = 0;
      private static int MSG_HELLO_COMPLETE = 1;
      private HandlerThread ht;
      private Handler mHtHandler;
      private Handler mUiHandler;
      private boolean helloReady = false;
      public HandlerThreadExample2(){
          ht = new HandlerThread("The new thread");
          ht.start();
          Log.d(App.TAG, "UI: handler thread started");
          mUiHandler = new Handler(){
              public void handleMessage(Message msg){
                  if (msg.what == MSG_HELLO_COMPLETE){
                      Log.d(App.TAG, "UI Thread: received notification of sleep completed ");
                      helloReady = true;              }
              }
          };
          mHtHandler = new Handler(ht.getLooper()){
              public void handleMessage (Message msg){
                  if (msg.what == MSG_START_HELLO){
                      Log.d(App.TAG, "handleMessage " + msg.what + " in " + Thread.currentThread() + " now sleeping");
                      try {
                          Thread.sleep(2000);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      Log.d(App.TAG, "Woke up, notifying UI thread...");
                      mUiHandler.sendEmptyMessage(MSG_HELLO_COMPLETE);
                  }
              }
          };
      }
      public void sendLongHello(){
          if (helloReady){
              Log.d(App.TAG, "sending hello " + Thread.currentThread());      
              mHtHandler.sendEmptyMessage(MSG_START_HELLO);
              helloReady = false;
          } else {
              Log.e(App.TAG, "Cannot do hello yet - not ready");
          }
      }
  }
  </code></pre>