Do we need invoking Looper.prepare() when invoking Handler

I need to understand about the Looper. Looper will consult appropiate handler to to send and process Message and Runnable objects associated with a thread's MessageQueue.

By default, a thread does not have a message loop associated with it, hence doesn’t have a Looper either. To create a Looper for a thread and dedicate that thread to process messages serially from a message loop, you can use the Looper class.

The following is my code I don't invoke Looper explicitly

Thread background2 = new Thread(new Runnable() {

            @Override
            public void run() {

            for ( int i = 0; i < 5; i++) {
               final int v =i;
                    try {   Thread.sleep(1000);
                  handler.post(new Runnable() {
                     @Override
                     public void run() {
                        txt.setText(txt.getText() +  "Thread 2 current i : " + String.valueOf(v) +System.getProperty("line.separator"));
                     }
                  });
                    } catch (Exception e) {
                        Log.v("Error", e.toString());
                    }
                }

            }
        });

Does it mean that the task/runnable is not put in the queue? what's the difference of above code with this

Thread background3 = new Thread(new Runnable() {

            @Override
            public void run() {
     Looper.prepare();
            for ( int i = 0; i < 5; i++) {
               final int v =i;
                    try {   Thread.sleep(1000);
                  handler.post(new Runnable() {
                     @Override
                     public void run() {
                        txt.setText(txt.getText()+ "Thread 3 set : " + String.valueOf(v) +System.getProperty("line.separator"));
                     }
                  });
                    } catch (Exception e) {
                        Log.v("Error", e.toString());
                    }
                }
Looper.loop();
            }
        });

both of them accessing a same handler. They both work fine.

Upvotes: 2

Views: 898

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Creating a Looper for a Thread means you're setting up that Thread to receive messages from other Threads. Both of your examples are behaving exactly the same because the you're not sending anything to the Thread in the second example. That is, the background3's Looper isn't really being used.

In both examples, you're posting a Runnable to a Handler that was created for the main Thread's Looper. You're not creating that Handler for, e.g., background2. That Handler belongs to the main Thread and its Looper, and anything you post to it will be put into the main queue, and run on the main Thread.

The only difference in your examples is that the second Thread has a Looper, and you could post to it, if you wanted to. To do that, you would create another Handler that belonged to background3's Looper, and post to that. You're not doing that, though, so the second Thread just continues to run without doing anything else.

A Thread doesn't need a Looper simply to post to another Thread's Handler, which is really all that your examples are doing. That other Thread - the main Thread, in this case - has already prepared and started its Looper. You're just sending Runnables to it, and you don't need a Looper of your own to do that.

Upvotes: 2

Related Questions