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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With