title: How do I ensure another Thread's Handler is not null before calling it?
body: <p>My program threw a NullPointerException the other day when it tried to use a Handler created on another thread to send that thread a message. The Handler created by the other thread was not yet created, or not yet visible to the calling thread, despite the calling thread having already called start on the other thread. This only happens very rarely. Almost every test run does not get the exception.</p>
  
  <p>I was wondering what the best way is to avoid this problem for sure with minimal complication and performance penalty. The program is a game and very performance sensitive, especially once it is running. Therefore I try to avoid using synchronization after setup, for example, and would prefer to avoid spinning on a variable at any time.</p>
  
  <p>Background:<br>
  In Android the Handler class may be used to "enqueue an action to be performed on a different thread than your own". Documentation here:<br>
  <a href="http://developer.android.com/intl/de/reference/android/os/Handler.html" rel="noreferrer">http://developer.android.com/intl/de/reference/android/os/Handler.html</a></p>
  
  <p>The Handler must be created on the thread where it will be used. So creating it in the constructor of a thread, which is run by the thread creating that thread, is not an option.</p>
  
  <p>When the Handler is for a thread other than the UI thread, the Looper class must also be used:<br>
  <a href="http://developer.android.com/intl/de/reference/android/os/Looper.html" rel="noreferrer">http://developer.android.com/intl/de/reference/android/os/Looper.html</a></p>
  
  <p>The documentation gives this example of using the two classes for this purpose:</p>
  
  <pre><code>class LooperThread extends Thread {
      public Handler mHandler;
  
      public void run() {
          Looper.prepare();
  
          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };
  
          Looper.loop();
      }
  }
  </code></pre>
  
  <p>My very ugly workaround currently looks like this:</p>
  
  <pre><code>public class LooperThread extends Thread {
  
      public volatile Handler mHandler;
  
      public final ArrayBlockingQueue&lt;Object&gt; setupComplete = new ArrayBlockingQueue&lt;Object&gt;(1);
  
      public void run() {
          Looper.prepare();
  
          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };
  
          setupComplete();
  
          Looper.loop();
      }
  
      public void waitForSetupComplete() {
          while ( true ) {
              try {
                  setupComplete.take();
                  return;
              } catch (InterruptedException e) {
                  //Ignore and try again.
              }
          }
      }
  
      private void setupComplete() {
          while( true ) {
              try {
                  setupComplete.put(new Object());
                  return;
              } catch (InterruptedException e) {
                  //Ignore and try again.
              }        
          }
      }
  
  }
  </code></pre>
  
  <p>With the code in the creating thread looking like this:</p>
  
  <pre><code>    LooperThread otherThread = new LooperThread();
      otherThread.start();        
      otherThread.waitForSetupComplete();
      otherThread.mHandler.sendEmptyMessage(0);
  </code></pre>
  
  <p>Are there any better solutions? Thanks.</p>