3
runOnUiThread(new Runnable(){

    @Override
    public void run() {

         System.out.println("print out from runOnUIThread.");
    }

});

System.out.println("print out in main thread.");


**Output:**
print out from runOnUIThread.
print out in main thread.



Basically runOnUiThread will be used in background thread, I am doing this for testing only.

The code above execute in Activity onCreate method.

From the output, the result is not what I expect. I am thinking that, since runOnUiThread post the runnable block to main thread, and the current execution context is in the main thread already, so runOnUiThread should be scheduled after "print out in main thread", but why the result doesn't show like that? Do I interpret it wrongly? Can anyone kindly explain?


Edit:

Oh I should read the API first. Anyway, why this confuse me is because, in iOS, the similar mechanism behave differently:

dispatch_async(dispatch_get_main_queue(), ^{
           NSLog(@"Main thread from Dispatch.");
});

NSLog(@"Main thread.");

The output of above is reversed.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • Well, are you actually using the `runOnUiThread()` on a background thread? If not, then that method will simply run the `run()` method of the `Runnable` on the current main thread. – user Mar 06 '14 at 07:26

2 Answers2

5

runOnUiThread: Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

Activity onCreate() method also runs in UI thread. First runOnUiThread code will run then rest of the code.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
2

Referenced

To use runOnUiThread() when you want to update your UI from a Non-UI Thread. i.e. If you want to update your UI from a background Thread. You can also use Handler for the same thing. It Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components(Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those applications are performed in this thread.

For instance, lets say your application is a single Activity class. Then all of the lifecycle methods and most of your event handling code is run in this UIThread. These are methods like OnCreate, OnPause, OnDestroy, OnClick, etc. Additionally, this is where all of the updates to the UI are made. Anything that causes the UI to be updated or changed HAS to happen on the UI thread.

When you explicitly spawn a new thread to do work in the background, this code is not is not run on the UIThread. So what happens if the this background thread needs to do something that changes the UI? This is what the runOnUiThread is for. Actually you're supposed to use a Handler ; HERE it provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the RunOnUiThread method.

Community
  • 1
  • 1
GrIsHu
  • 29,068
  • 10
  • 64
  • 102