问题
I usually use the method
runOnUiThread (new Runnable () {
@Override
public void run () {
}
});
to launch some prcess in the main thread. just recently I discovered this one
new Handler(Looper.getMainLooper()).post(new Runnable () {
@Override
public void run () {
// this will run in the main thread
}
});
My question is what is the difference between the two methods and which one is the best to use?
回答1:
Both are actually same. Both runOnUiThread
and Handler#post
runs the passed Runnable
in the UI Thread.
FYI, you can also execute any Runnable
on UI Thread with the help of any View
by calling the method View#post(runnable)
.
Since all approaches uses Handler
internally, all are same and there won't be any difference in using any of these.
来源:https://stackoverflow.com/questions/52152906/what-is-the-difference-between-runonuithread-method-and-handler-which-one-is-th