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.