Handler-Looper implementation in Android
Asked Answered
Y

2

3
  1. I have Activity with Handler (UI thread)
  2. I start new Thread and make handler.post(new MyRunnable()) - (new work thread)

Android documentation said about post method: "Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached."

Handler attached to UI thread. How android can run runnable in the same UI thread without new thread creation?

Is new thread will be created using Runnable from handler.post()? Or it's only run() method will be called from Runnable subclass?

Yachtsman answered 15/3, 2011 at 18:38 Comment(0)
W
3

Handler attached to UI thread.

Correct.

How android can run runnable in the same UI thread without new thread creation?

Any thread, including the main application ("UI") thread, can call post() on Handler (or on any View, for that matter).

Whicker answered 15/3, 2011 at 18:56 Comment(7)
Is new thread will be created using Runnable from handler.post()? So message with runnable will be taken from queue and new thread will be started? I can't understand what's "The runnable will be run on the thread to which this handler is attached" mean. How runnable can be run in the same thread without new thread creation?Yachtsman
@androidev Alex: No. There is only one main application ("UI") thread per application. post() will run your Runnable on that thread.Whicker
@androidev Alex: I think so, but to be completely honest, I do not understand your question fully.Whicker
@Whicker : The OP is trying to say that, when you are creating a runnable, it's run method will be run by a separate thread. Then HOW can you run that task in the UI thread . Because when you call the runnable.start(), a new thread is created right?Drape
@Ashwin: "The OP is trying to say that, when you are creating a runnable, it's run method will be run by a separate thread." -- that is incorrect. A Runnable can be run in a separate thread; it is not automatically run in a separate thread. "Because when you call the runnable.start(), a new thread is created right?" -- no. This is standard Java, the way it has behaved for the past 15 years or so.Whicker
@Whicker : Thanks for replying. Can you please point me to some documentation which provides with the full details. Like, when a new thread is created and when not.Drape
@Ashwin: Generally speaking, a new thread is created when you call new Thread(). Some Android stuff will create threads, notably AsyncTask, but that is documented on the class itself (e.g., on AsyncTask), not in some central location.Whicker
S
5

Here's a rough pseudroidcode example of how to use handlers - I hope it helps :)

class MyActivity extends Activity {

    private Handler mHandler = new Handler();

    private Runnable updateUI = new Runnable() {
        public void run() {
            //Do UI changes (or anything that requires UI thread) here
        }
    };

    Button doSomeWorkButton = findSomeView();

    public void onCreate() {
        doSomeWorkButton.addClickListener(new ClickListener() {
            //Someone just clicked the work button!
            //This is too much work for the UI thread, so we'll start a new thread.
            Thread doSomeWork = new Thread( new Runnable() {
                public void run() {
                    //Work goes here. Werk, werk.
                    //...
                    //...
                    //Job done! Time to update UI...but I'm not the UI thread! :(
                    //So, let's alert the UI thread:
                    mHandler.post(updateUI);
                    //UI thread will eventually call the run() method of the "updateUI" object.
                }
            });
            doSomeWork.start();
        });
    }
}
Syman answered 15/3, 2011 at 20:33 Comment(4)
So UI thread only call the run() method of the "updateUI" object? And UI thread will not create new thread and call start() to do run() method code in new thread?Yachtsman
As I understand it, that's correct. (CommonsWare seems to be saying the same thing, above, and he speaks with some authority. xD)Syman
@Syman : The OP is trying to say that, when you are creating a runnable, it's run method will be run by a separate thread. Then HOW can you run that task in the UI thread . Because when you call the runnable.start(), a new thread is created right?Drape
@Ashwin: I don't think so...it's when you make a new Thread object and call thread.start() that a new thread is created. You normally initialize a new Thread object with a Runnable, but you can do other things with Runnables, too--like posting them to a Handler.Syman
W
3

Handler attached to UI thread.

Correct.

How android can run runnable in the same UI thread without new thread creation?

Any thread, including the main application ("UI") thread, can call post() on Handler (or on any View, for that matter).

Whicker answered 15/3, 2011 at 18:56 Comment(7)
Is new thread will be created using Runnable from handler.post()? So message with runnable will be taken from queue and new thread will be started? I can't understand what's "The runnable will be run on the thread to which this handler is attached" mean. How runnable can be run in the same thread without new thread creation?Yachtsman
@androidev Alex: No. There is only one main application ("UI") thread per application. post() will run your Runnable on that thread.Whicker
@androidev Alex: I think so, but to be completely honest, I do not understand your question fully.Whicker
@Whicker : The OP is trying to say that, when you are creating a runnable, it's run method will be run by a separate thread. Then HOW can you run that task in the UI thread . Because when you call the runnable.start(), a new thread is created right?Drape
@Ashwin: "The OP is trying to say that, when you are creating a runnable, it's run method will be run by a separate thread." -- that is incorrect. A Runnable can be run in a separate thread; it is not automatically run in a separate thread. "Because when you call the runnable.start(), a new thread is created right?" -- no. This is standard Java, the way it has behaved for the past 15 years or so.Whicker
@Whicker : Thanks for replying. Can you please point me to some documentation which provides with the full details. Like, when a new thread is created and when not.Drape
@Ashwin: Generally speaking, a new thread is created when you call new Thread(). Some Android stuff will create threads, notably AsyncTask, but that is documented on the class itself (e.g., on AsyncTask), not in some central location.Whicker

© 2022 - 2024 — McMap. All rights reserved.