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?

mobiledev Alex
  • 2,228
  • 2
  • 28
  • 30

2 Answers2

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();
        });
    }
}
Cephron
  • 1,577
  • 1
  • 19
  • 26
  • 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? – mobiledev Alex Mar 15 '11 at 21:00
  • As I understand it, that's correct. (CommonsWare seems to be saying the same thing, above, and he speaks with some authority. xD) – Cephron Mar 15 '11 at 21:09
  • @Cephron : 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? – Ashwin Sep 02 '12 at 13:20
  • @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. – Cephron Sep 04 '12 at 15:35
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).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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? – mobiledev Alex Mar 15 '11 at 19:34
  • 1
    @androidev Alex: No. There is only one main application ("UI") thread per application. `post()` will run your `Runnable` on that thread. – CommonsWare Mar 15 '11 at 19:36
  • @androidev Alex: I think so, but to be completely honest, I do not understand your question fully. – CommonsWare Mar 15 '11 at 20:07
  • @CommonsWare : 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? – Ashwin Sep 02 '12 at 13:19
  • @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. – CommonsWare Sep 02 '12 at 13:32
  • @CommonsWare : 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. – Ashwin Sep 02 '12 at 14:55
  • @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. – CommonsWare Sep 02 '12 at 14:59