Handler class and the timing of when its message queue is emptied
<p>I was curious about the nature of the <code>handleMessage()</code> and <code>sendMessage()</code> behavior of the <a href="http://developer.android.com/reference/android/os/Handler.html" rel="nofollow"><code>Handler</code></a> class. I want to be able to send message to another thread in such a way that the destination thread can process the message queue when it wants to. It seems, however, that the message is processed by <code>handleMessage()</code> practically as soon as it's sent.</p>

<p>I'm trying to design a game loop thread that does something like this:</p>

<pre><code>void run(){
   while (gameIsActive){
     handleInput();
     updateGameState();
   }
}

handleInput(){
  //Remove an item from the handler's message queue
  //which can be UI events (click, touch, etc.)
}
</code></pre>

<p>However, I as soon as sendMessage() is called (from the parent/calling thread), the Handler.handleMessage() is processed (in the child/receiving thread), even if the child/receiving thread is blocking in a while loop. </p>

<p>I've seen this problem solved in other games by using a thread-safe list (<code>ConcurrentLinkedQueue</code>). The UI thread just posts events to this queue, and the game loop can remove the events as it seems fit. I just assumed the Handler class was designed for this purpose. It seems it's more intended for asynchronous callbacks to the parent thread. </p>
