title: android send message to worker thread
body: <p>I have a thread where I need to periodically perform some checks, get files from the web, and send messages to the main UI thread. I even need to use UI thread parameters (like the map visible area) on each loop of the worker thread. So I suppose that i need to implement bidirectional communication between UIthread and workerThread.
  Another problem is that I need to save the identifier of each marker added to the map. I want to save the result of map.addMarker inside my custom array stored in my worker thread. this means that from the uithread, where i update the map, i should tell the workerThread to update the array of markers.. </p>
  
  <p>This is a sample of my actual worker thread:</p>
  
  <pre><code> class MyThread extends Thread {
       private Handler handler;
       private  MainActivity main;
  
       public MyThread (MainActivity mainClass, Handler handlerClass) {
           this.main=mainClass;
           this.handler = handlerClass;   
       }
  
       @Override
       public void run(){
              while(true){
                 sleep(2000);
                 //do my stuffs
                 //....
                 //prepare a message for the UI thread
                 Message msg = handler.obtainMessage();
                 msg.obj= //here i put my object or i can even use a bundle
                 handler.sendMessage(msg); //with this i send a message to my UI thread
  
              }
       }
   }
  </code></pre>
  
  <p>My actual problem is that when the UI thread ends processing the message received from the worker thread i should perform an action on the worker thread.</p>
  
  <p>I thought 2 solutions: </p>
  
  <p>1)wait on the worker thread till the message has been processed by the UI thread</p>
  
  <p>2)process the message on the UI thread and then send a message to the worker thread.</p>
  
  <p>I don't know how to do the solution1, so i tried the solution2. I tried adding a looper to my worker thread (RUN sub), this way:</p>
  
  <pre><code> class MyThread extends Thread {
       private Handler handler;
       private  MainActivity main;
  
       public MyThread (MainActivity mainClass, Handler handlerClass) {
           this.main=mainClass;
           this.handler = handlerClass;   
       }
  
       @Override
       public void run(){
              Looper.prepare();
              mHandler = new Handler() {
                   public void handleMessage(Message msg) {
                        // Act on the message received from my UI thread doing my stuff
                   }
              };
              Looper.loop();
              while(true){
                 sleep(2000);
                 //do my stuffs
                 //....
                 //prepare a message for the UI thread
                 Message msg = handler.obtainMessage();
                 msg.obj= //here i put my object or i can even use a bundle
                 handler.sendMessage(msg); //with this i send a message to my UI thread
  
              }
       }
   }
  </code></pre>
  
  <p>The problem is that after the Looper.loop() no line of code is executed. I read that this is normal. I read many articles but I didn't understand how should I allow the execution of my while loop, and simultaneously process messages coming from my UI thread.
  I hope the problem is clear. Suggest me the best solution. </p>