2

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.

  • A `Thread` does not need a `Looper` to post to another `Thread`'s `Handler`, if that's what you're asking. The `Handler`s in your examples are for other `Thread`s, whichever `Thread`s you're creating them on. – Mike M. Jul 07 '16 at 01:53
  • Hello, I'm confused now so what's the difference on my second example, there I'm invoking Looper.prepare( ) explicitly, but without it, it still works – Plain_Dude_Sleeping_Alone Jul 07 '16 at 01:58
  • Creating a `Looper` for a `Thread` means that _other_ `Thread`s can post to it. The `Looper` in your example isn't really doing anything, since you're not posting anything to a `Handler` created there, which is why you don't see a difference. – Mike M. Jul 07 '16 at 02:03
  • Mike, whats `that other Threads can post to it` in this context?? could you put it as answer please, – Plain_Dude_Sleeping_Alone Jul 07 '16 at 02:07
  • Well, there are plenty of answers here that already cover this, so I'd rather find a duplicate than post a repeat answer. Anyway, in your second example, since you're starting a `Looper` in that `Thread`, you could create a `Handler` for that `Looper`, pass it to another `Thread`, and then that other `Thread` can post to it. Your example `Thread`s are posting `Runnable`s to whichever other `Thread` the `Handler`s are created for. They're not posting them to themselves. – Mike M. Jul 07 '16 at 02:14
  • Think of a `Thread` as a neighborhood, a `Looper` as a post office, and a `Handler` as a mailman from that post office. If a neighborhood (`Thread`) doesn't have a post office (`Looper`), you can't send letters there. If it does have a post office (`Looper`), then you can send letters to that neighborhood (`Thread`), but you have to use their mailman (`Handler`). – Mike M. Jul 07 '16 at 02:20
  • No problem. I'll try to find a duplicate that explains it well. – Mike M. Jul 07 '16 at 02:26
  • These two have some pretty good explanations: http://stackoverflow.com/questions/12877944/what-is-the-relationship-between-looper-handler-and-messagequeue-in-android http://stackoverflow.com/questions/7597742/what-is-the-purpose-of-looper-and-how-to-use-it Don't get discouraged. It can take a little while to get a grasp on this stuff. – Mike M. Jul 07 '16 at 02:32
  • Ok Mike thanks for helping me I'll patiently understanding this. and read the posts you posted. – Plain_Dude_Sleeping_Alone Jul 07 '16 at 02:41
  • Actually, I was just thinking, now that I reread those other answers, they don't really directly address your particular question or situation. I think I will put together an answer for you, to better explain than I can in comments. Gimme a little bit. – Mike M. Jul 07 '16 at 02:46

1 Answers1

2

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.

Mike M.
  • 38,532
  • 8
  • 99
  • 95